How can i Write a program that will read in a number and then print out a triangle of #s using nested loops? - python-3.x

Heres the code of the program.
def go( num ):
print(" ")
go( 1 )
go( 2 )
go( 3 )
go( 4 )
go( 9 )
go( 12 )
Now i am really having trouble understanding how to wrtie it so it goes through the list.
This is the code ive done so far
> def go( num ):
> row = 0
for i in range(row + 1):
for j in range(i):`
print(i, end=" ")
print("*")
go( 1 )
go( 2 )
go( 3 )
go( 4 )
go( 9 )
go( 12 )
I am currently getting a
"builtins.NameError: name 'row' is not defined" error,anything would help.
Also yeah i am kind of rushing this

I think you are looking something like this
def go(num):
for i in range(num+1):
for j in range(i):
print(i, end=" ")
print("")
go(2)
1
2 2
go(9)
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9

Related

Program to produce the following pattern

I am trying to print
2
1 3
2 4 6
1 3 5 7
2 4 6 8 10
I am not sure how to do it ,I could use print (...) And write everything but that is just stupid.
Is there a better way to do this?
for i in range(0, 5):
for j in range(0, i+1):
if i % 2 == 0:
print((j+1)*2, end=' ')
else:
print((j*2)+1, end=' ')
print()
maybe you can try this:
>>> def pattern_gen(layer):
... for i in range(layer):
... pattern=''
... start=(i+1)%2+1
... end=(i+(i+1)%2+1)*2
... for j in range(start,end,2):
... pattern+=f'{j} '
... print(pattern)
...
>>> pattern_gen(5)
2
1 3
2 4 6
1 3 5 7
2 4 6 8 10

Tracing sorting algorithms

I am trying to trace the changes in selection sort algorithm with python, Here's a piece of my code and what I've tried, the problem I am facing is printing the results in a table-like format
l = [2,5,1,7,9,5,3,0,-1]
iterat = 1
print('Iteration' + '\t\t\t' + 'Results')
for i in range(1, len(l)):
val_to_sort = l[i]
while l[i-1] > val_to_sort and i > 0:
l[i-1], l[i] = l[i], l[i-1]
i -= 1
print(iterat, '\t\t\t', l[0:iterat + 1],'|',l[iterat:])
iten += 1
from the code above, I am obtaining the following results:
But I am trying to obtain such results
Unident print one level to the left, so it is inside the for block instead of the while block.
Use join and map to print the lists as a string
You can use enumerate instead of manually incrementing iterat
def format_list(l):
return ' '.join(map(str, l))
l = [2,5,1,7,9,5,3,0,-1]
print('Iteration' + '\t\t\t' + 'Results')
for iterat, i in enumerate(range(1, len(l)), 1):
val_to_sort = l[i]
while l[i-1] > val_to_sort and i > 0:
l[i-1], l[i] = l[i], l[i-1]
i -= 1
print(iterat, '\t\t\t', format_list(l[0:iterat + 1]),'|', format_list(l[iterat:]))
Outputs
Iteration Results
1 2 5 | 5 1 7 9 5 3 0 -1
2 1 2 5 | 5 7 9 5 3 0 -1
3 1 2 5 7 | 7 9 5 3 0 -1
4 1 2 5 7 9 | 9 5 3 0 -1
5 1 2 5 5 7 9 | 9 3 0 -1
6 1 2 3 5 5 7 9 | 9 0 -1
7 0 1 2 3 5 5 7 9 | 9 -1
8 -1 0 1 2 3 5 5 7 9 | 9
I can't help you with the Cyrillic text though ;)

Groovy to separate every 5th Character with a character

I need a groovy script to insert one separator line after every 10th unique number
Example:
Input:
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
7
8
9
7
Output:
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
///////////////
7
8
9
7
Blockquote
​def arr = [1,2,3,4,5,6,7,8,9,9,9,9,9,9,9,9,0,7,8,9,7]
def map = [:]
arr.each{
println it
map[it] = (map.containsKey("$it") ? map[it] : 1)+1
if( map.size() == 10 ){
println "/////"
map = [:]
}
}
one way:
def numbers = """\
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
7
8
9
7""".readLines().collect { it as Integer }
def unique = [] as Set
def seen = [0] as Set // 0 - don't add delimiter at start
def result = numbers.inject([]) { acc, val ->
def s = unique.size()
// if we are on a 10 multiple of unique numbers seen
// and we have not already added a delimiter for this multiple
if (!(s % 10 || s in seen)) {
acc << '///////////////'
seen << s
}
unique << val
acc << val
}
// result will now contain the original list of numbers,
// interleaved with the delimiter every 10th unique number
result.each {
println it
}
which results in:
~> groovy solution.groovy
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
///////////////
7
8
9
7

Python how to print 1D list as 2D

I want to print a list [1,2,3,4,5,6,7,8,9] as
1 2 3
4 5 6
7 8 9
Here is my code
for i,j in enumerate(list):
if i is not 0 and i % 3==0:
print()
else:
print(j,end=" ")
My result is
1 2 3
5 6
8 9
Can someone help explain why this happen and give me some advise?
You should print the list item j unconditionally instead of doing it only when you are not printing a newline:
l = [1,2,3,4,5,6,7,8,9]
for i,j in enumerate(l):
if i is not 0 and i % 3==0:
print()
print(j,end=" ")
You can do:
tgt=[1,2,3,4,5,6,7,8,9]
n=3
print('\n'.join([' '.join(map(str, sl)) for sl in [tgt[i:i+n] for i in range(0,len(tgt),n)]]))
Prints:
1 2 3
4 5 6
7 8 9

Need numbering above each column of a python grid

I need to modify a program the program 'x' so that it outputs the original grid, but each row and column is labeled with it's respective number. i.e. column 1 has a first, row 1 has a 1 first. I have figured out how to put numbering in front of the rows, but cannot figure out how to modify the program so that the columns are numbered.
'x'
import random
aGrid = []
SIZE = 10
for r in range (0,SIZE,1):
aGrid.append ([])
for c in range (0,SIZE,1):
element = random.randint(0,9)
aGrid[r].append (element)
for r in range (0,SIZE,1):
for c in range (0,SIZE,1):
print(aGrid[r][c], end="")
print()
Here is my attempt at getting numbering in front of the rows
import random
aGrid = []
SIZE = 11
for r in range (0,SIZE - 1,1):
aGrid.append ([])
aGrid[r].append (r )
aGrid[r].append (" ")
for c in range (0,SIZE + 1,1):
element = random.randint(0,9)
aGrid[r].append (element)
for r in range (0,SIZE - 1,1):
for c in range (0,SIZE + 1,1):
print(aGrid[r][c], end="")
print()
You don't need to change aGrid. Just print the row and column numbers in the print loop:
import random
aGrid = []
SIZE = 10
for r in range (SIZE):
aGrid.append ([])
for c in range (SIZE):
element = random.randint(0,9)
aGrid[r].append (element)
print(" ", end="")
for c in range (SIZE):
print(c, end="")
print()
for i, r in enumerate(range(SIZE)):
print(i, end="")
for c in range(SIZE):
print(aGrid[r][c], end="")
print()
Output:
0123456789
01958724133
17006217440
21488953544
35615572045
49849348546
54207744418
63316678723
76651582077
85713263320
91939404287
A version that starts counting from 1 and has some spaces for better readability:
import random
aGrid = []
SIZE = 10
for r in range (SIZE):
aGrid.append ([])
for c in range (SIZE):
element = random.randint(0,9)
aGrid[r].append (element)
print(" ", end=" ")
for c in range (1, SIZE + 1):
print(c, end=" ")
print()
for i, r in enumerate(range(SIZE), 1):
print('{:2d}'.format(i), end=" ")
for c in range(SIZE):
print(aGrid[r][c], end=" ")
print()
Output:
1 2 3 4 5 6 7 8 9 10
1 3 0 4 6 3 9 3 3 6 3
2 5 6 1 6 6 3 2 5 6 1
3 6 9 0 5 0 7 1 1 7 7
4 7 4 3 9 0 9 1 0 7 8
5 5 1 1 1 1 7 1 4 4 8
6 4 5 8 1 6 3 6 2 8 6
7 4 1 0 5 7 4 5 6 6 4
8 4 5 5 4 3 3 0 9 2 1
9 3 6 7 0 0 9 5 8 5 9
10 3 1 2 3 5 0 1 6 2 9

Resources