Okay so i'm trying to make a code that will read in a positive odd integer and output an inverse pyramid starting with that number and descending to 1 and cutting off the first and last digit in the next line and so on. So if i entered 7 it would display:
7654321
65432
543
4
The i 'th row contains n-(2i-2) but I'm not sure how to use that.
Thanks for your help.
This is what I have so far:
#include <iostream>
using namespace std;
int main()
{
int n,i,j;
cout << "Enter a positive odd number: " << endl;
cin >> n ;
i=n;
while(n%2 ==0)
{
cout<< "Invalid number." << endl;
cout<< "Enter a positive odd number: " << endl;
cin >> n ;
}
for(i=n; i<=n && i>0 ; i--)
{
for(j=i; j<=i; j--)
{
cout<< i%10 ;
}
cout<<endl;
}
return(0);
}
Number the character positions on screen like this:
+----+----+----+----+----+----+----+
| 0 0| 0 1| 0 2| 0 3| 0 4| 0 5| 0 6|
+----+----+----+----+----+----+----+
| 1 0| 1 1| 1 2| 1 3| 1 4| 1 5| 1 6|
+----+----+----+----+----+----+----+
| 2 0| 2 1| 2 2| 2 3| 2 4| 2 5| 2 6|
+----+----+----+----+----+----+----+
| 3 0| 3 1| 3 2| 3 3| 3 4| 3 5| 3 6|
+----+----+----+----+----+----+----+
and check what goes in there
+----+----+----+----+----+----+----+
| 7 | 6 | 5 | 4 | 3 | 2 | 1 |
+----+----+----+----+----+----+----+
| | 6 | 5 | 4 | 3 | 2 | |
+----+----+----+----+----+----+----+
| | | 5 | 4 | 3 | | |
+----+----+----+----+----+----+----+
| | | | 4 | | | |
+----+----+----+----+----+----+----+
Now find the relation between x, y, the value to print, and the initial number.
Related
I have data looking like this:
| ID |OpID|
| -- | -- |
| 10 | 1 |
| 10 | 2 |
| 10 | 4 |
| 11 |null|
| 12 | 3 |
| 12 | 4 |
| 13 | 1 |
| 13 | 2 |
| 13 | 3 |
| 14 | 2 |
| 14 | 4 |
Here OpID 4 means 1 and 2.
I would like to count the different occurrences of 1, 2 and 3 in OpID of distinct ID.
If the counts of OpID having 1 would be 4, 2 would be 4, 3 would be 2.
If ID has OpID of 4 but already has data of 1, 2 it wouldn't be counted. But if 4 exists and only 1 (2) is there, count for 2 (1) would be incremented.
The expected output would be:
|OpID|Count|
| 1 | 4 |
| 2 | 4 |
| 3 | 2 |
(Going to be using the results in a column chart)
Hope this makes sense...
edit: there are other columns too and an ID and OpID can be duplicated hence need to do a groupby clause before.
PySpark 2.4.0
How to train a model which has multiple target columns?
Here is a sample dataset,
+---+----+-------+--------+--------+--------+
| id|days|product|target_1|target_2|target_3|
+---+----+-------+--------+--------+--------+
| 1| 6| 55| 1| 0| 1|
| 2| 3| 52| 0| 1| 0|
| 3| 4| 53| 1| 1| 1|
| 1| 5| 53| 1| 0| 0|
| 2| 2| 53| 1| 0| 0|
| 3| 1| 54| 0| 1| 0|
+---+----+-------+--------+--------+--------+
id, days and product are the feature columns. In order to train using PySpark ML - MLPC, i've converted the features into feature vectors.
Here is the code,
from pyspark.ml.linalg import Vectors
from pyspark.ml.feature import VectorAssembler
assembler = VectorAssembler(
inputCols=['id', 'days', 'product'],
outputCol="features")
output = assembler.transform(data)
and i've feature column as below,
+---+----+-------+--------+--------+--------+--------------+
| id|days|product|target_1|target_2|target_3| features|
+---+----+-------+--------+--------+--------+--------------+
| 1| 6| 55| 1| 0| 1|[1.0,6.0,55.0]|
| 2| 3| 52| 0| 1| 0|[2.0,3.0,52.0]|
| 3| 4| 53| 1| 1| 1|[3.0,4.0,53.0]|
| 1| 5| 53| 1| 0| 0|[1.0,5.0,53.0]|
| 2| 2| 53| 1| 0| 0|[2.0,2.0,53.0]|
| 3| 1| 54| 0| 1| 0|[3.0,1.0,54.0]|
+---+----+-------+--------+--------+--------+--------------+
Now if i take each target columns as single label, i'll end up creating 3 models. But is there a way to convert all 3 targets(they are binary - 0 or 1) into labels.
For example if i take each target column separately then my MLPC layer will be like,
target_1 >> layers = [3, 5, 4, 2]
target_2 >> layers = [3, 5, 4, 2]
target_3 >> layers = [3, 5, 4, 2]
Since the target column contains only 0 or 1. Can i create a layer like below,
layers = [3, 5, 4, 3]
3 output for each target columns, they should give an output of 0 or 1 from every output neuron.
from pyspark.ml.classification import MultilayerPerceptronClassifier
trainer = MultilayerPerceptronClassifier(maxIter=100, layers=layers,blockSize=128, seed=1234)
I tried to combine all targets into single label,
assembler_label = VectorAssembler(
inputCols=['target_1', 'target_2', 'target_3'],
outputCol="label")
output_with_label = assembler_label.transform(output)
And the resulting data looks like,
+---+----+-------+--------+--------+--------+--------------+-------------+
| id|days|product|target_1|target_2|target_3| features| label|
+---+----+-------+--------+--------+--------+--------------+-------------+
| 1| 6| 55| 1| 0| 1|[1.0,6.0,55.0]|[1.0,0.0,1.0]|
| 2| 3| 52| 0| 1| 0|[2.0,3.0,52.0]|[0.0,1.0,0.0]|
| 3| 4| 53| 1| 1| 1|[3.0,4.0,53.0]|[1.0,1.0,1.0]|
| 1| 5| 53| 1| 0| 0|[1.0,5.0,53.0]|[1.0,0.0,0.0]|
| 2| 2| 53| 1| 0| 0|[2.0,2.0,53.0]|[1.0,0.0,0.0]|
| 3| 1| 54| 0| 1| 0|[3.0,1.0,54.0]|[0.0,1.0,0.0]|
+---+----+-------+--------+--------+--------+--------------+-------------+
When i tried to fit the data,
model = trainer.fit(output_with_label)
i got an error,
IllegalArgumentException: u'requirement failed: Column label must be of type numeric but was actually of type struct<type:tinyint,size:int,indices:array<int>,values:array<double>>.'
So, is there a way to handle data like this?
I have a data frame which is shown below (Except Price2 variable):
+------+-----------+----------+-------+------+-------------+
| Name | Day | Time | Price | Size | Price2 |
+------+-----------+----------+-------+------+-------------+
| A | 24-Mar-08 | 10:30:01 | 1 | 3 | 0.333333333 |
| A | 24-Mar-08 | 10:30:01 | 4 | 4 | 1.777777778 |
| A | 24-Mar-08 | 10:30:01 | 3 | 2 | 0.666666667 |
| A | 24-Mar-08 | 11:03:12 | 1 | 4 | 0.8 |
| A | 24-Mar-08 | 11:03:12 | 4 | 1 | 0.8 |
| A | 25-Mar-08 | 10:30:01 | 3 | 4 | 2 |
| A | 25-Mar-08 | 10:30:01 | 8 | 2 | 2.666666667 |
| A | 25-Mar-08 | 11:13:59 | 3 | 2 | 0.428571429 |
| A | 25-Mar-08 | 11:13:59 | 2 | 4 | 0.571428571 |
| A | 25-Mar-08 | 11:13:59 | 5 | 5 | 1.785714286 |
| A | 25-Mar-08 | 11:13:59 | 3 | 3 | 0.642857143 |
| A | 25-Mar-08 | 11:59:01 | 1 | 5 | 1 |
| B | 24-Mar-08 | 10:30:01 | 3 | 6 | 2.571429 |
| B | 24-Mar-08 | 10:30:01 | 4 | 1 | 0.571428 |
| B | 24-Mar-08 | 11:30:01 | 3 | 2 | 2 |
| B | 24-Mar-08 | 11:30:01 | 5 | 1 | 1.666667 |
| B | 25-Mar-08 | 11:30:01 | 7 | 3 | 1.909090909 |
| B | 25-Mar-08 | 11:30:01 | 4 | 6 | 2.181818182 |
| B | 25-Mar-08 | 11:30:01 | 2 | 2 | 0.363636364 |
| B | 25-Mar-08 | 12:00:00 | 6 | 2 | 6 |
+------+-----------+----------+-------+------+-------------+
I want to calculate Price2 in Stata, which is Price multiplied by Size and divided by the sum of Size for each second.
My solution is similar to that of #Andrey Ampilogov, and like him I don't see where all your results come from.
clear
input str1 Name str9 (Day Time) Price Size Price2
A 24-Mar-08 "10:30:01" 1 3 0.333333333
A 24-Mar-08 "10:30:01" 4 4 1.777777778
A 24-Mar-08 "10:30:01" 3 2 0.666666667
A 24-Mar-08 "11:03:12" 1 4 0.8
A 24-Mar-08 "11:03:12" 4 1 0.8
A 25-Mar-08 "10:30:01" 3 4 2
A 25-Mar-08 "10:30:01" 8 2 2.666666667
A 25-Mar-08 "11:13:59" 3 2 0.428571429
A 25-Mar-08 "11:13:59" 2 4 0.571428571
A 25-Mar-08 "11:13:59" 5 5 1.785714286
A 25-Mar-08 "11:13:59" 3 3 0.642857143
A 25-Mar-08 "11:59:01" 1 5 1
B 24-Mar-08 "10:30:01" 3 6 1.8
B 24-Mar-08 "10:30:01" 4 1 0.4
B 24-Mar-08 "11:30:01" 3 2 0.6
B 24-Mar-08 "11:30:01" 5 1 0.5
B 25-Mar-08 "11:30:01" 7 3 1.909090909
B 25-Mar-08 "11:30:01" 4 6 2.181818182
B 25-Mar-08 "11:30:01" 2 2 0.363636364
B 25-Mar-08 "12:00:00" 6 2 6
end
egen den = total(Size), by(Name Day Time)
gen wanted = (Price * Size)/den
list, sepby(Name Day Time)
+------------------------------------------------------------------------+
| Name Day Time Price Size Price2 den wanted |
|------------------------------------------------------------------------|
1. | A 24-Mar-08 10:30:01 1 3 .3333333 9 .3333333 |
2. | A 24-Mar-08 10:30:01 4 4 1.777778 9 1.777778 |
3. | A 24-Mar-08 10:30:01 3 2 .6666667 9 .6666667 |
|------------------------------------------------------------------------|
4. | A 24-Mar-08 11:03:12 1 4 .8 5 .8 |
5. | A 24-Mar-08 11:03:12 4 1 .8 5 .8 |
|------------------------------------------------------------------------|
6. | A 25-Mar-08 10:30:01 3 4 2 6 2 |
7. | A 25-Mar-08 10:30:01 8 2 2.666667 6 2.666667 |
|------------------------------------------------------------------------|
8. | A 25-Mar-08 11:13:59 3 2 .4285714 14 .4285714 |
9. | A 25-Mar-08 11:13:59 2 4 .5714286 14 .5714286 |
10. | A 25-Mar-08 11:13:59 5 5 1.785714 14 1.785714 |
11. | A 25-Mar-08 11:13:59 3 3 .6428571 14 .6428571 |
|------------------------------------------------------------------------|
12. | A 25-Mar-08 11:59:01 1 5 1 5 1 |
|------------------------------------------------------------------------|
13. | B 24-Mar-08 10:30:01 3 6 1.8 7 2.571429 |
14. | B 24-Mar-08 10:30:01 4 1 .4 7 .5714286 |
|------------------------------------------------------------------------|
15. | B 24-Mar-08 11:30:01 3 2 .6 3 2 |
16. | B 24-Mar-08 11:30:01 5 1 .5 3 1.666667 |
|------------------------------------------------------------------------|
17. | B 25-Mar-08 11:30:01 7 3 1.909091 11 1.909091 |
18. | B 25-Mar-08 11:30:01 4 6 2.181818 11 2.181818 |
19. | B 25-Mar-08 11:30:01 2 2 .3636364 11 .3636364 |
|------------------------------------------------------------------------|
20. | B 25-Mar-08 12:00:00 6 2 6 2 6 |
+------------------------------------------------------------------------+
At first, generate a sum of size for each group of Name - Day - Time. Then do the rest of the math - multiply the size by the price and divide by the sum of the sizes:
bys Name Day Time: egen sumPrice = total(Size)
gen Price2 = Price * Size / sumPrice
And also check a group of Name="B", Day = "24-Mar-08", Time = "10:30:01". The Price2 from your example and re-calculated Price2 do not match. Other values match.
How to increase column values from:
1 | 1 | 7.317073
2 | 1 | 14.634146
3 | 1 | 24.390244
4 | 2 | 7.317073
5 | 2 | 14.634146
6 | 2 | 24.390244
To:
1 | 1 | 7.317073
2 | 1 | 14.634146
3 | 1 | 24.390244
4 | 2 | 7.317073
5 | 2 | 14.634146
6 | 2 | 24.390244
7 | 3 | 7.317073
8 | 3 | 14.634146
9 | 3 | 24.390244
10 | 4 | 7.317073
11 | 4 | 14.634146
12 | 4 | 24.390244
I'm using Open Office.
Assuming that the top left corner is A1, set the fourth row such:
A4: =A3+1
B4: =roundup(A4/3)
C4 =C1
And pull them up to row 12
For ColumnA simply selecting the first three rows, grabbing the fill handle (black square at the bottom right of the range) and dragging down to suit should be sufficient.
An alternative here to ROUNDUP is, in B1 and copied down:
=INT((ROW()-1)/3)+1
For ColumnC as for ColumnA but with Crl depressed.
I would like to check if a value has appeared in some previous row of the same column.
At the end I would like to have a cumulative count of the number of distinct observations.
Is there any other solution than concenating all _n rows and using regular expressions? I'm getting there with concatenating the rows, but given the limit of 244 characters for string variables (in Stata <13), this is sometimes not applicable.
Here's what I'm doing right now:
gen tmp=x
replace tmp = tmp[_n-1]+ "," + tmp if _n > 1
gen cumu=0
replace cumu=1 if regexm(tmp[_n-1],x+"|"+x+",|"+","+x+",")==0
replace cumu= sum(cumu)
Example
+-----+
| x |
|-----|
1. | 12 |
2. | 32 |
3. | 12 |
4. | 43 |
5. | 43 |
6. | 3 |
7. | 4 |
8. | 3 |
9. | 3 |
10. | 3 |
+-----+
becomes
+-------------------------------+
| x | tmp |
|-----|--------------------------
1. | 12 | 12 |
2. | 32 | 12,32 |
3. | 12 | 12,32,12 |
4. | 43 | 3,32,12,43 |
5. | 43 | 3,32,12,43,43 |
6. | 3 | 3,32,12,43,43,3 |
7. | 4 | 3,32,12,43,43,3,4 |
8. | 3 | 3,32,12,43,43,3,4,3 |
9. | 3 | 3,32,12,43,43,3,4,3,3 |
10. | 3 | 3,32,12,43,43,3,4,3,3,3|
+--------------------------------+
and finally
+-----------+
| x | cumu|
|-----|------
1. | 12 | 1 |
2. | 32 | 2 |
3. | 12 | 2 |
4. | 43 | 3 |
5. | 43 | 3 |
6. | 3 | 4 |
7. | 4 | 5 |
8. | 3 | 5 |
9. | 3 | 5 |
10. | 3 | 5 |
+-----------+
Any ideas how to avoid the 'middle step' (for me that gets very important when having strings in x instead of numbers).
Thanks!
Regular expressions are great, but here as often elsewhere simple calculations suffice. With your sample data
. input x
x
1. 12
2. 32
3. 12
4. 43
5. 43
6. 3
7. 4
8. 3
9. 3
10. 3
11. end
end of do-file
you can identify first occurrences of each distinct value:
. gen long order = _n
. bysort x (order) : gen first = _n == 1
. sort order
. l
+--------------------+
| x order first |
|--------------------|
1. | 12 1 1 |
2. | 32 2 1 |
3. | 12 3 0 |
4. | 43 4 1 |
5. | 43 5 0 |
|--------------------|
6. | 3 6 1 |
7. | 4 7 1 |
8. | 3 8 0 |
9. | 3 9 0 |
10. | 3 10 0 |
+--------------------+
The number of distinct values seen so far is then just a cumulative sum of first using sum(). This works with string variables too. In fact this problem is one of several discussed within
http://www.stata-journal.com/sjpdf.html?articlenum=dm0042
which is accessible to all as a .pdf. search distinct would have pointed you to this article.
Becoming fluent with what you can do with by:, sort, _n and _N is an important skill in Stata. See also
http://www.stata-journal.com/sjpdf.html?articlenum=pr0004
for another article accessible to all.