Patterns in nested for loops - python-3.x

In python I need the output to be the following, I included the image of the output:
enter image description here
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
I've tried googling the answer but I don't get the exact pattern

Im not sure why you want pattern matching. You can achieve this result with a simple for loop.
for i in range(9):
for j in range(1,i+1):
print((i*j), end=" ")
print("\n")

this should give you the result you want.
_______ = 9
for __, ____ in enumerate(list(range(1,_______+1))):
_____ = f"{____}"
for ___ in range(1,__+1):
_____ = f"{_____} {____*(___+1)}"
print(_____)

Related

random number generator issue: removing None at the end [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I created a lottery number generator with Python 3.7. It shows however None at the end of each try. Here's my code.
import random
def lotto_gen():
n = 1
while n < 7:
print(random.randint(1, 45), end='\t')
n += 1
return
for numbers in range(100):
print(lotto_gen())
And the result goes like this:
6 12 42 37 13 44 None
36 31 32 41 4 30 None
20 31 38 42 14 19 None
8 18 29 22 34 29 None
26 34 15 1 20 38 None
10 17 28 35 22 38 None
23 34 42 22 4 43 None
25 16 17 36 17 4 None
44 8 20 1 43 43 None
29 32 9 2 8 5 None
16 44 35 17 42 10 None
5 1 39 28 21 40 None
35 25 12 31 23 21 None
13 25 9 10 41 7 None
12 34 14 36 27 5 None
32 30 12 5 41 14 None
23 30 5 30 7 9 None
38 25 6 17 17 20 None
12 1 13 10 30 32 None
15 1 3 23 28 6 None
1 2 24 33 36 31 None
28 13 42 39 9 39 None
41 44 2 9 41 34 None
25 19 30 26 8 44 None
39 36 44 4 22 7 None
7 44 29 38 1 8 None
37 6 44 6 41 11 None
29 29 23 40 23 36 None
25 39 30 40 40 4 None
28 14 33 4 15 34 None
41 35 7 26 30 24 None
10 34 26 45 12 10 None
32 6 45 16 24 18 None
14 7 8 26 32 4 None
22 43 40 3 20 31 None
6 42 38 11 18 20 None
6 40 5 18 25 29 None
37 19 26 19 45 41 None
39 8 17 19 17 22 None
I want to remove that None bool type. Can someone tell me how can I edit my code?
Rakesh has given the correct answer, but I would like to explain why your code isn't working. The problem seems to be that for a particular iteration, your code is only able to generate 6 random numbers. Take note, that you have initialized n=1, inside the function lotto_gen() and as the condition for executing the while loop is n<7, the code inside lotto_gen() executes only 6 times.
Now the reason why you receive None at the end is because you are trying to print the value returned by lotto_gen, but take note, that the return field inside your code's function is empty, hence None is returned by the function and hence that gets printed.
So for correcting the code you only need to initialize n as n=0, and to remove the appearance of the none, don't call the function inside a print statement, and create a list which contains the 7 values of each iteration and return it. So, you'll need to modify the code in this manner:
import random
def lotto_gen():
n = 0
a=[]
while n < 7:
a.append(random.randint(1, 45))
n += 1
return a
for numbers in range(100):
print(lotto_gen())
You can use this approach too and my code will execute faster as well! :P
This is one approach.
Ex:
import random
def lotto_gen():
return "\t".join(str(random.randint(1, 45)) for _ in range(6))
for numbers in range(100):
print(lotto_gen())

printing a string like a matrix

Trying to let the user input a number, and print a table according to the square of its size. Here's an example.
Size--> 3
0 1 2
3 4 5
6 7 8
Size--> 4
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Size--> 6
0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
18 19 20 21 22 23
24 25 26 27 28 29
30 31 32 33 34 35
Size--> 9
0 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44
45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62
63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80
Here's is the code that i have tried.
length=int(input('Size--> '))
size=length*length
biglist=[]
for i in range(size):
biglist.append(i)
biglist = [str(i) for i in biglist]
for i in range(0, len(biglist), length):
print(' '.join(biglist[i: i+length]))
but instead here's what i got
Size--> 3
0 1 2
3 4 5
6 7 8
Size--> 4
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Size--> 6
0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
18 19 20 21 22 23
24 25 26 27 28 29
30 31 32 33 34 35
As you can see the rows are not aligned properly like the example.
What's the simplest way of presenting it in a proper alignment? Thx :)
Using .format on string with right aligning.
And strlen is the number of characters required for each number.
length = int(input('Size--> '))
size = length*length
biglist = []
for i in range(size):
biglist.append(i)
biglist = [str(i) for i in biglist]
strlen = len(str(length**2-1))+1
for i in range(0, len(biglist), length):
# print(' '.join(biglist[i: i+length]))
for x in biglist[i: i+length]:
print(f"{x:>{strlen}}", end='')
print()

How to divide 1 column into 5 segments with pandas and python?

I have a list of 1 column and 50 rows.
I want to divide it into 5 segments. And each segment has to become a column of a dataframe. I do not want the NAN to appear (figure2). How can I solve that?
Like this:
df = pd.DataFrame(result_list)
AWA=df[:10]
REM=df[10:20]
S1=df[20:30]
S2=df[30:40]
SWS=df[40:50]
result = pd.concat([AWA, REM, S1, S2, SWS], axis=1)
result
Figure2
You can use numpy's reshape function:
result_list = [i for i in range(50)]
pd.DataFrame(np.reshape(result_list, (10, 5), order='F'))
Out:
0 1 2 3 4
0 0 10 20 30 40
1 1 11 21 31 41
2 2 12 22 32 42
3 3 13 23 33 43
4 4 14 24 34 44
5 5 15 25 35 45
6 6 16 26 36 46
7 7 17 27 37 47
8 8 18 28 38 48
9 9 19 29 39 49

Plotting in R in Linux Terminal

I have a text file that I converted into a numeric vector:
numbers <- scan("list_of_numbers.txt")
I then put it into a table:
t <- table(numbers)
Which outputs like this:
1 2 3 4 5 6 7 8 9 10 11
621266 496647 436229 394595 353249 305882 253983 199455 147380 102872 67255
12 13 14 15 16 17 18 19 20 21 22
41934 24506 13778 7179 3646 1778 816 436 217 114 74
23 24 25 26 27 28 29 30 31 32 33
49 44 26 21 19 21 20 14 9 17 14
34 35 36 37 38 39 40 41 42 43 44
7 11 9 14 3 5 8 4 4 2 3
45 46 47 55 56 60 62 63 69 70 72
2 1 2 2 2 1 1 1 3 2 1
78 82 85 93 95 114 125 265 331 350
1 1 1 1 1 1 1 1 1 1
How would I plot a line graph with x axis of numbers 1 - 25 and y axis the frequency values of the x axis all in the terminal window?
In addition, how can a plot like this (which is default saved as a .pdf file) be viewd in the linux terminal?
Most commands like less, cat, and xdg-open output a bunch of strange unreadable symbols.
You can use fbi, the linux framebuffer imageviewer to open pdf files in the linux console.
A small problem can be that it needs root privileges. It seems like it can not run through R using system, it complains about not being a linux console. But you can use it in the terminal like:
sudo fbi Rplots.pdf
As for the plotting part of your question you can just use something like:
plot(t, xlim = c(1, 25))
Hope it helps,
alex
I think it's very convenient to use txtplot::txtplot as follow:
> cat("1 2 3 4 5 6", file = "list_of_numbers.txt", sep = "\n")
> numbers <- scan("list_of_numbers.txt")
Read 6 items
> t <- table(numbers)
> txtplot(t)
You can install it just by this command:
install.packages('txtplot')
I found that Jupyter may be the best wheel for us to handle that, and we can equip that following this tutorial: Embed Graphs In Jupyter Notebooks in R
References:
How To Install R on Ubuntu 18.04
scan

How to calculate 95th percentile in Excel 2010 [duplicate]

This question already has answers here:
Calculate Percentile in Excel 2010
(3 answers)
Closed 9 years ago.
I am trying to calculate how many calls came back in 95 percentile of time. Below is my Result Set. I am working with Excel 2010
Milliseconds Number
0 1702
1 15036
2 14262
3 13190
4 9137
5 5635
6 3742
7 2628
8 1899
9 1298
10 963
11 727
12 503
13 415
14 311
15 235
16 204
17 140
18 109
19 83
20 72
21 55
22 52
23 35
24 33
25 25
26 15
27 18
28 14
29 15
30 13
31 19
32 23
33 19
34 21
35 20
36 25
37 26
38 13
39 12
40 10
41 17
42 6
43 7
44 8
45 4
46 7
47 9
48 11
49 12
50 9
51 9
52 9
53 8
54 10
55 10
56 11
57 3
58 7
59 7
60 2
61 5
62 7
63 5
64 5
65 2
66 3
67 2
68 1
70 1
71 2
72 1
73 4
74 1
75 1
76 1
77 3
80 1
81 1
85 1
87 2
93 1
96 1
100 1
107 1
112 1
116 1
125 1
190 1
356 1
450 1
492 1
497 1
554 1
957 1
Just some background what does above information means-
1702 calls came back in 0 milliseconds
15036 calls came back in 1 milliseconds
14262 calls came back in 2 milliseconds
etc etc
So to calculate the 95th percentile from the above data, I am using this formula in excel 2010-
=PERCENTILE.EXC(IF(TRANSPOSE(ROW(INDIRECT("1:"&MAX(H$2:H$96))))<=H$2:H$96,A$2:A$96),0.95)
Can anyone help me whether the way I am doing in Excel 2010 is right or not?
I am getting 95th percentile as 10 by using the above scenario.
Thanks for the help.
that's essentially the same question you asked here and the formula I suggested. As per my last comments in that question - that formula should work OK as long as you use CTRL+SHIFT+ENTER correctly. I get 10 as the answer for this example using that formula.
I think you can verify manually that that is indeed the correct answer. If you have a running total in an adjacent column then you can see where the 95th percentile is reached......

Resources