Hey guys, need some help with the following assignment. The Language required is
ID: 3932624 • Letter: H
Question
Hey guys, need some help with the following assignment. The Language required is Python
1. 1. Write a program that prints all of the digits is a string. For example, if the string is “2 Temple 16”, the output will be: 2 1 6
2. Assume that we have a list of integers. Write a program to calculate the average of the numbers in the list. Do NOT use the sum function. For example, if the list is t=[5,10,35], your program should print 25.
3. Write a program that reverses a string. For example, if string=“Harry”, the program stores “yrraH” in variable reverse. Then, the program prints “yrraH”. (Hint: you can use concatenation to create variable reverse).
Explanation / Answer
Question 1:
s = raw_input("Enter the string: ")
for i in range(0, len(s)):
if s[i] >= '0' and s[i] <= '9':
print s[i]+" ",
Output:
sh-4.3$ python main.py
Enter the string: 2 Temple 16
2 1 6
Question 2:
t=[5,10,35]
l = len(t)
add = 0
for i in t:
add = add + i
average = add/float(l)
print "Average: ", average
Output:
sh-4.3$ python main.py
Average: 16.6666666667
Question 3:
s = raw_input("Enter the string: ")
reverse = ""
for i in range(len(s)-1, -1, -1):
reverse = reverse + s[i]
print "Reverse string is: ",reverse
Output:
sh-4.3$ python main.py
Enter the string: Harry
Reverse string is: yrraH