5)
Define a function called remainder _is_even which receives two positive integer numbers as parameters: num and div. This function should return a boolean value. The value to be returned should be True if the remainder of dividing num by div is even and it should return False otherwise.
As an example, the following code fragment:
print (remainder_is_even(23,2))
should produce the output:
False
6)
Define a function first_last_repeated which receives as input parameter a string (orig) with at least one character, and a positive integer number (repet). The function should return a new string, which has the first character in the original repeated repet times followed by a space and then followed by the last character in the original string also repeated repet times.
As an example, the following code fragment:
print (first_last_repeated("abcd",4))
should produce the output:
aaaa dddd
7)
Define a function named double_add_digits_in_string(...) which receives a string as a parameter and returns the sum of the digits multiplied by two. A solution using a loop is expected.
As an example, the following code fragment:
total = double_add_digits_in_string("xx1xx2xx3xx") print (total)
should produce the output:
12
8)
This function digits_stars(...) receives as input one digit (that is, an integer number greater or equal than 0 and less or equal than 9). The function should return a string. The string will have , alternating, a digit and a "*" symbol, starting with the digit 0, then a star, then the digit 1, then a star, and so on, alternating until reaching the input digit and one star symbol to end the string.
As an example, the following code fragment:
print (digits_stars(3))
should produce the output:
0*1*2*3*
# 5
def remainder_is_even(num, div):
return (num % div) % 2 == 0
# 6
def first_last_repeated(orig, repet):
return orig[0] * repet + ' ' + orig[-1] * repet
# 7
def double_add_digits_in_string(s):
total = 0
for ch in s:
if ch.isdigit():
total += int(ch)
return 2 * total
# 8
def digits_stars(n):
result = ''
for i in range(n + 1):
result += str(i) + "*"
return result
# Testing the functions here. ignore/remove the code below if not required
print(remainder_is_even(23, 2))
print(first_last_repeated("abcd", 4))
total = double_add_digits_in_string("xx1xx2xx3xx")
print(total)
print(digits_stars(3))
5) Define a function called remainder _is_even which receives two positive integer numbers as parameters: num...
Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a new string which will have the middle character/s in the string repeated as many times as the length of the input (original) string. Notice that if the original string has an odd number of characters there is only one middle character. If, on the other hand, if the original string has an even number of characters then there will...
Define a function named how_many_substr_of_string(...) which receives two parameters, the first parameters is a list with strings (name it listst) and the second parameter is a single string (name it st). The function should return a number, indicating how many of the strings in the list listst are substrings in the string st As an example, the following code fragment: listst = ["a","bc","dd"] st = "abc" res = how_many_substr_of_string(listst,st) print (res) should produce the output: 2 language:Python
Define a function named how_many_substr_of_string(...) which receives two parameters, the first parameters is a list with strings (name it listst) and the second parameter is a single string (name it st). The function should return a number, indicating how many of the strings in the list listst are substrings in the string st As an example, the following code fragment: listst = ["a","bc","dd"] st = "abc" res = how_many_substr_of_string(listst,st) print (res) should produce the output: 2 Language Python
PYTHON define a function called fav_colours(): which receives a list of strings of lowercasecolours: the list some words will repeat, one colour or many, long list or short, Will always be lowercase - return a list of lists containing unique colours and integer numbers describing the amount of times those colours repeated As an example, the following code fragment: colours = ['blue', 'red', 'red', 'red', 'green'] colours , numbers = fav_colours(colours) print(colours) print(numbers) should produce the output: ['blue', 'red', 'green']...
D.1 [3] Define a function called funD1...) that receives a string and returns a new string with all the characters in the original string in an EVEN position. As an example, the following code fragment: print (funD1('abcde')) should produce the output: ace D.2 [6] Define a function funD2(...) which receives a list ist that contains single letters, single digits and single special characters and the list contains at least one element of each type). The function returns a string that...
Create a function named list_numbers_in_words(...) which receives as a parameter a positive integer number and returns a list with the words corresponding to each digit, in the same order as the digits appear in the number. As an example, the following code fragment: res = list_numbers_in_words(5438) print(res) should produce the output: ['five', 'four', 'three', 'eight']
Define a function funD2(...) which receives a list lst that contains single letters, single digits and single special characters (and the list contains at least one element of each type). The function returns a string that contains the last letter and the last special character of the list, where each is repeated as many times as the sum of all digits in the list. As an example, the following code fragment: lst = ["a","b","c", 1, 2, "$","%"] print (funD2(lst)) should...
Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument, and returns an integer. This function should add the digits of a string that are in an even position. If there are no digits, the function should return -1. As an example, the following code fragment: string = "a12b056jk"; result=AddEvenPosDigs(string); print(result) should produce the output: 8
Create a function named first_is_smaller(...) which receives two integer parameters (n1 and n2) and returns a Boolean value True if the value in the first parameter is smaller than the second and False otherwise. As an example, the following code fragment: print(first_is_smaller(10,20)) should produce the output: True Language: Python
Define a function named double_add_digits_in_string(...) which receives a string as a parameter and returns the sum of the digits multiplied by two. A solution using a loop is expected. As an example, the following code fragment: total = double_add_digits_in_string("xx1xx2xx3xx") print (total) should produce the output: 12