Question

In: Computer Science

You are not allowed to import anything • You are not allowed to use slicing •...

You are not allowed to import anything • You are not allowed to use slicing • You are not allowed to use sets or dictionaries • You are not allowed to use any string method • You are not allowed to use anything that hasn’t been covered in class • No built-in function except range() and len() is allowed • From list methods, you are allowed to use .append(), .insert(), .remove() or del. Please do not ask on piazza whether you can use .sort(), sorted(), .index(), .count() etc.

mashup(lst) [20pts] Description: Creates a new string that is based on the provided list. Each number in the list specifies how many characters from the string that follows belong in the resulting string. Parameters: lst is a list of variable size, that contains alternating ints and strings Assumptions: When a pair of values doesn’t make sense, throw out that pair. When you have an empty string in the list, move on to the next pair. When you have a number that is larger than the length of the string, move on to the next pair, etc. Return value: the new string that is generated from the replacements

examples:

mashup([3, '', 1, 'QHnwq', 5, 'UgJoSwnkq']) == 'QUgJoS'

mashup([2, 'abc', 1, 'def']) → 'abd'

mashup([3, 'rate', 2, 'inside', 1, 'goat']) → 'rating

Solutions

Expert Solution

In this program, we have to form a string from the first n characters of each string given in the list, where n is given in the index preceding these strings.

Inorder to do this, we will be seperating the lenghts and strings from the list and store them into two seperate lists, named lens and strings.

Then, we will define an empty string finalString. This string will store the string to be returned

Then, loop through each element in lens and strings. If the length of the current string in 0, then move on to next iteration. if the current element in lens in greater than the length of current string, then move on to next iteration. Otherwise, append the first n charaters in strings to finalString, here n is the current element in lens

after the loop , return finalString

program:

def mashup(lst):
finalString = ""
lens = lst[0:-1:2]
strings = lst[1::2]
for i in range(len(lens)):
if len(strings[i])==0:
continue
if lens[i] > len(strings[i]):
continue
finalString += strings[i][:lens[i]]

return finalString

sample function call:

print(mashup([3, '', 1, 'QHnwq', 5, 'UgJoSwnkq']))
print(mashup([2, 'abc', 1, 'def']))
print(mashup([3, 'rate', 2, 'inside', 1, 'goat']))

output:


Related Solutions

Java Program // DO NOT USE BREAK OR CONTINUE :) You are allowed to use the...
Java Program // DO NOT USE BREAK OR CONTINUE :) You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method CANNOT use break or continue moveXDownLeft takes a char and a two dimensional array of char as input and returns nothing: The method should find the first occurrence of the char in the array (searching from "top" to "bottom" and "left" to "right"), and...
Java program - you are not allowed to use arithmetic operations such as division (/), multiplication,...
Java program - you are not allowed to use arithmetic operations such as division (/), multiplication, or modulo (%) to extract the bits. In this exercise use only logic bit-wise operations. Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you CANNOT use the arithmetic division by...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1098, -1 and 34 are not. Complete the intQ2(intQ2_input) function that takes an input integer...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes only a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use arrays...
How and when do you use the Monte Carlo analysis? Does it have anything to do...
How and when do you use the Monte Carlo analysis? Does it have anything to do with understanding the probability of a risk occurring as well?
You are allowed to use any sources you like. Make sure to cite any sources you...
You are allowed to use any sources you like. Make sure to cite any sources you use (any reasonable citation style is fine). Do not copy text from any source (short, cited quotes OK). Question: In a species of deer, you find an allele that causes females to always produce an equal number of male and female offspring. Present an argument affirming that this sex ratio is an evolutionary stable strategy. Then, try to present a plausible scenario in this...
Activity 4 Answer the following questions. You are allowed to use online calculators such as Stat...
Activity 4 Answer the following questions. You are allowed to use online calculators such as Stat Trek Binomial, Poisson, ect calculators, just make sure to note the values you entered in your answer. A similar question has previously been answered but it's incorrect which is why I am reposting my own version of the question. I would prefer if the calculator was used where applicable but show any work to solve the problem as well. Suppose you are designing a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT