All codes needs to be in python using while loop not for loo
All codes needs to be in python using while loop not for loop:
If you answer the question please label which question number you answered thanks!!
Also, Do not worry about testing the code I have the code to test it just focus on the questions.
First question:
Second Question:
Third Question:
Fourth Question:
Fifth question:
Six Question
| Problem 1: | Write code that takes a non-empty list of integers from the user, as well as a width. Return a new list where the original list has been converted into a two dimensional list with the specified width, padded with zeroes at the end as needed. You may not use any built-in functions/methods besides len() and .append(). (This was a homework problem) |
| Problem 2: | Write code that takes two strings from the user, and returns what is left over if the second string is removed from the first. The second string to be removed is guaranteed to be less than four letters long. |
Solution
Here is th code for you:
#!/usr/bin/python
#Write code that takes a non-empty list of integers from the user, as well as a width.
#Return a new list where the original list has been converted into a two dimensional list
#with the specified width, padded with zeroes at the end as needed.
#You may not use any built-in functions/methods besides len() and .append().
def ListTo2D(list, width):
TwoD = [[]]
i = 0
j = 0
k = 0
TwoD.append([])
while i < len(list):
TwoD[j].append(0)
TwoD[j][k] = list[i]
k += 1
if k % width == 0:
k = 0
j += 1
i += 1
if(k == 0 and i < len(list)):
TwoD.append([])
while k < width:
TwoD[j].append(0)
k += 1
return TwoD
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print list
newList = ListTo2D(list, 5)
print newList
If you need any refinements, just get back to me.
