I am trying to arrange slashes in print statement to Graph like this
Below is skeleton of my code
y = input("Enter numbers (seperated by space): ")
y_arr = []
y_list = y.split()
for n in range(len(y_list)):
y_arr.append(int(y_list[n]))
space_no = 0
for x in range(len(y_arr)):
value = y_arr[x]
for z in range(value):
if (x%2 == 0):
print (" "*space_no,"\\")
space_no += 1
else:
print (" "*space_no,"/")
space_no += 1
Input statement take numbers and put it into list, then the slashes are generated based on the value of list item. For exa: if value is 2 then 2 slashes will be printed. Even index values go up, odd index values go down. Print statement is printing every character in new line but I want to arrange them like shown in graph.
How to achieve this?
code edited to Python 3
That sounds like homework, but here it is.
y_list = '0 3 1 2 3 6 2 3 6 2 3'.split()
y_arr = []
for n in range(len(y_list)):
y_arr.append(int(y_list[n]))
def cumulative(lists):
cu_list = []
length = len(lists)
cu_list = [sum(lists[0:x:1]) for x in range(0, length+1)]
return cu_list[1:]
space_up = 0
space_dn = 0
last_idx = -1
lines = ['']*max(cumulative([-x if i%2 == 0 else x for i,x in enumerate(y_arr)]))
for x in range(len(y_arr)):
value = y_arr[x]
for z in range(value):
if (x%2 == 0):
lines[last_idx] += (" "*space_dn+"\\")
space_dn = len(lines[last_idx])-len(lines[last_idx-1])
last_idx -= 1
space_up = 0
else:
last_idx += 1
lines[last_idx] += (" "*space_up+"/")
try:
space_up = len(lines[last_idx])-len(lines[last_idx+1])
except IndexError:
pass
space_dn = 0
print('\n'.join(lines[::-1]))
Related
number_count = int(input("Number count example (3 = 3 * 3): "))
range_list = [*range(number_count ** 2)]
y = 0
range_list[:] = 'X'
for x in range_list:
y += 1
print(range_list[x], end=' ')
if y == number_count:
print('\n')
y = 0
I tried this but 'range_list[:] = "X"' give me error. My desire is basic i want to change all elements in list to 'X' or 'O'.
I have a list like Y Y Y N Y. I need to figure out the longest streak. The code
print(max(map(len,input().replace(' ','').split('N'))))
works but I need to do this without max and split.
The following code works for Y Y Y N and for N N N (results zero) but fails for Y Y N Y Y Y.
I need to somehow introduce a counter that is not set to 0 after the streak is broken and remember the largest streak
Y = 'Y'
temp = 0
streak = False
entry = input()
mylist = list(entry)
for i in range(0, len(mylist)):
if mylist[i] == Y:
streak = True
temp = temp + 1
else:
streak = False
#print (temp)
#temp = 0
print(temp)
I codded the general case of unknown input (not only N and Y), and without using built-in functions max or split by building an histogram and find out the maximum in it (take into calculation the case of several times the same letter as input), e.g:
input: insert string: yyttyyyy
entry = input("insert string: ")
mylist = list(entry)
hist = {} # string as key and int as value
lastItem = None
iterations_num = 0
for item in mylist:
if item == lastItem:
if item + str(iterations_num) in hist.keys():
hist[item + str(iterations_num)] = hist[item + str(iterations_num)]+1
else:
lastItem = item
iterations_num = iterations_num+1
hist[item + str(iterations_num)] = 1
max = 0
for amount in hist.values():
if amount>max:
max = amount
print(hist)
print(max)
output:
{'y1': 2, 't2': 2, 'y3': 4}
4
All you're missing is to reset the counter (temp) and check if it's greater than the current maximum (which you are missing) in the else part. The boolean streak variable is really unnecessary:
longest = 0
streak = 0
entry = input()
mylist = entry.split()
for elem in mylist:
if elem == 'Y':
streak += 1
else:
longest = max(longest, streak)
streak = 0
print(max(longest, streak))
I tried it a lot and I want to get occurrence of 4 without using count function
n = int(input()) #number of input
for i in range(n): #range
l = [] #list
x = int(input()) #input
while (x > 0): #innserting input into list
y = x % 10
x = x / 10
l.append(y)
z = 0
for i in l:
if (i == 4): # calculating no of occurrence
z = z + 1
print(z)
Here's a much simpler way of solving it.
number = int(input("Enter Number: "))
fours = [] #create list to store all the fours
for x in str(number): #loop through the number as a string
if x == '4': #check if the character if 4
fours.append(x) #add it to the list of fours
print("Number of fours: " + str(len(fours))) #prints the length of the array
I'm trying to solve a problem I was given for homework and really feel like I'm overthinking the algorithm and hoping someone here can push me in the right direction.
I'm going to be given an input txt file which will look like this :
1 // n number of graphs
4 // n number of vertices for graph 1
4 // n number of edges for graph 1
1 2 // edges given in pairs
2 3
2 4
3 4
And I'm supposed to use this data to crate n number of adjacency matrices representing the graphs. I then need to implement 3 methods on the data in the adjacency matrices:
findLongestPath() which will return the longest path in the graph
findShortestPath() which will return the shortest path in the graph
totalNumberPaths() which will return distinct number of paths in graph
I'm having difficulty implementing the first two parts fine. This is what I have so far:
def main():
numGraphs = input()
for x in xrange(0, numGraphs):
numVerts = input()
numEdges = input()
adjMat = [[0 for x in xrange(numVerts)] for x in xrange(numVerts)]
for x in xrange(0, numEdges):
edges = raw_input()
i, padding, j = edges.rpartition(" ")
i = int(i)
j = int(j)
i -= 1
j -= 1
adjMat[i][j] = 1
numPaths = [0 for x in xrange(numVerts)]
numPaths[0] = 1
longest_path = 1
shortest_path = numVerts
for i in xrange(0, numVerts):
current_path = 0
for j in xrange(0, numVerts):
if adjMat[i][j] == 1:
numPaths[j] += numPaths[i]
current_path += 1
if current_path > longest_path:
longest_path = current_path
if current_path < shortest_path:
shortest_path = current_path
print "shortest: %d, longest: %d, total %d" % (shortest_path, longest_path, numPaths[numVerts-1])
if __name__ == "__main__":
main()
Obviously when it hits a row of 0s the shortest_path updates to 0 and doesn't work. Plus it won't work when initialized to a 0. If I could get some pseudo code or maybe help with the longer or shorter method I'm sure I could write the opposite or maybe I'm totally off base.
Thanks for any input.
Edit:
So i figured it out. Here's my finished code in case anyone has a similar problem and needs help.
numGraphs = input()
for x in xrange(0, numGraphs):
numVerts = input()
numEdges = input()
adjMat = [[0 for x in xrange(numVerts)] for x in xrange(numVerts)]
for x in xrange(0, numEdges):
edges = raw_input()
i, padding, j = edges.rpartition(" ")
i = int(i)
j = int(j)
i -= 1
j -= 1
adjMat[i][j] = 1
numPaths = [0 for x in xrange(numVerts)]
numPaths[0] = 1
currentPath = [0 for x in xrange(numVerts)]
maxPath = 1
minPath = numVerts -1
for i in xrange(0, numVerts):
for j in xrange(1, numVerts):
if adjMat[i][j] == 1:
numPaths[j] += numPaths[i]
currentPath[j-i] += 1
if (currentPath[j-i] is not 0):
minPath = currentPath[j-i]
maxPath = max(currentPath)
print "shortest: %d, longest: %d, total %d" % (minPath, maxPath, numPaths[numVerts-1])
Figured it out. Here is my final solution.
numGraphs = input()
for x in xrange(0, numGraphs):
numVerts = input()
numEdges = input()
adjMat = [[0 for x in xrange(numVerts)] for x in xrange(numVerts)]
for x in xrange(0, numEdges):
edges = raw_input()
i, padding, j = edges.rpartition(" ")
i = int(i)
j = int(j)
i -= 1
j -= 1
adjMat[i][j] = 1
numPaths = [0 for x in xrange(numVerts)]
numPaths[0] = 1
currentPath = [0 for x in xrange(numVerts)]
maxPath = 1
minPath = numVerts -1
for i in xrange(0, numVerts):
for j in xrange(1, numVerts):
if adjMat[i][j] == 1:
numPaths[j] += numPaths[i]
currentPath[j-i] += 1
if (currentPath[j-i] is not 0):
minPath = currentPath[j-i]
maxPath = max(currentPath)
print "shortest: %d, longest: %d, total %d" % (minPath, maxPath, numPaths[numVerts-1])
I am doing a simple script for self learning in python where the scripts in turn finds the 1000th prime number but I get a syntax error.
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:(counter = counter + 1 and integer = integer + 1)
print (y)
when it comes to the ='s assignment right after the ELSE operator and I don't understand why it won't let me add one to both the counter and integer when this has worked in other iteration scenario
In python you can't make an assignment inside an expresion, to avoid misspellings between = and ==. So you must do that in two lines:
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:
counter += 1
integer += 1
print (y)
try this
else:
counter = counter + 1
integer = integer + 1
In python, assignment to variable has no Boolean value. and mean Boolean operator not do this and this.
so you need to split the statements.
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:
counter += 1
integer += 1
print (y)