create the following functions in python, and explain steps thank you. h) Write
ID: 3606602 • Letter: C
Question
create the following functions in python, and explain steps thank you.
h) Write a function that takes in a string of non-negative integers separated by a space and returns the sum of the integers in the string (e.g., Input: “12 23 56”, output: 91)
i) Write a function that takes in a list of string representation of integers and returns a list of those integers (e.g., Input: [‘12’, ‘45’, ‘78’], output: [12, 45, 78])
j) Write a function that takes in a list of integers and returns a True if the given list of integers is in increasing order. (Each integer is strictly larger than the integer before it. Use a while loop for this question).
k)Write a function that takes in a list and changes each elements in the list to a negative if the element at that location was a positive number, positive if the number at that location was a negative number, and “SOMethiNG” for anything else example: input my_list: [1, -2 True, “a”] output:my_list:[ -1, 2 , “SOMethiNG”, “SOMethiNG”]
Explanation / Answer
Code:
def return_sum(string):
l = string.split(" ")
l = [int(x) for x in l]
return sum(l)
def return_ints(l):
return [int(x) for x in l]
def is_increasing(l):
boolean = False
for i in range(len(l)-1):
if (l[i] < l[i+1]):
boolean = True
else:
boolean = False
return boolean
def change_values(l):
for i in range(len(l)):
if (type(l[i]) is not str):
if (l[i] > 0 or l[i] < 0):
l[i] = -l[i]
else:
l[i] = "SOMethiNG"
return l
string = "12 34 56"
l1 = ['12','34','56']
l2 = [1,2,3,4,2]
l3 = [1, -2 ,"True", "a"]
print(return_sum(string))
print(return_ints(l1))
print(is_increasing(l2))
print(change_values(l3))
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.