Question

For Python, I am a little confused on the use of reduce,map, and lambda. My assignment wants me to not use .join() and write a SINGLE LINE function to join each letter in a list with another string.(See problem below).

Using reduce, map, and lambda, write the single-expression function myJoin(L, sep) that takes a non-empty list L and a string

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for adding list of characters to string without using join.

if the version of python is above 3 we have to import reduce function or else no need to import it.

from functools import reduce #importing reduce since my python version is 3.8
def myJoin(L,sep): #function for list to string changing
    return(reduce(lambda x,y:x+y,L) )#single line reduce and lambda function and returning the string
L=['r','e','d','u','c','e','l','a','m','d','a','u','s','e'] //taking list
print(myJoin(L,',')) //calling the function and printing the string which is return from function.

Code and ouptut:

from functools import reduce def myJoin (L, sep): return (reduce (lambda x, y:xty,L)) LFIr,e,d,,c,e,l,a, m,d,a,u,s,e

How reduce function works is lets assume a small list['h','e','l','l',o'] now if we pass this list to lambda function which contains reduce it will perform rolling computation means first reduce function will take 'h','e' and concatenates to 'he' and then it will take 'he' and 'l' and concatenates to 'hel' after that 'hel','l' concatenates to'hell' finally 'hell','o' will concatenate and return the string 'hello'.

lets assume one more example if we have a list containing 5 elements [1,2,3,4,5] if we want to product those elements then we can use reduce it will take first 1 & 2 and computes 1*2=2 and then it will take the result of first one and 3 and computes the result i.e 2*3=6 after that 6*4=24 and then 24*5=120.

Note : If you have any queries please post a comment thanks a lot.

Add a comment
Know the answer?
Add Answer to:
For Python, I am a little confused on the use of reduce,map, and lambda. My assignment wants me t...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this task you're going to write a function that joins strings from a list. The function calle...

    I need help with this python programming assignment please double check the indentations For this task you're going to write a function that joins strings from a list. The function called join ) has one formal parameter and one optional parameter. Recall that an optional parameter is like end or sep in the print( function. You don't have to use it because there's a default value. For the print () function, e.g., \'n' (newline) is the default value for end....

  • Write the function in python and show the code 2. Write a function jscore (s1, s2)...

    Write the function in python and show the code 2. Write a function jscore (s1, s2) that takes two strings s1 and s2 as inputs and returns the Jotto score of s1 compared with s2 -.e., the number of characters in s1 that are shared by s2. The positions and the order of the shared characters within each string do not matter. Repeated letters are counted multiple times, as long as they appear multiple times in both strings. For example...

  • I really need help with this python programming assignment Program Requirements For part 2, i need...

    I really need help with this python programming assignment Program Requirements For part 2, i need the following functions • get floats(): It take a single integer argument and returns a list of floats. where it was something like this def get_floats(n):   lst = []   for i in range(1,n+1):     val = float(input('Enter float '+str(i)+': '))     lst.append(val)   return lst • summer(): This non-void function takes a single list argument, and returns the sum of the list. However, it does not use...

  • IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand...

    IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....

  • In the lectures about lists we have seen some Python code examples that involve the processing of...

    In the lectures about lists we have seen some Python code examples that involve the processing of lists containing weather statistics. The original source of the values shown in the slides was taken from part of the Environment Canada website that serves up historical data: http://climate.weather.gc.ca/historical_data/search_historic_data_e.html    Data can be provied by that website using the Comma Separated Value (CSV) format which is stored in text files normally using a CSV suffix. We will not work directly with such files in...

  • My Python file will not work below and I am not sure why, please help me...

    My Python file will not work below and I am not sure why, please help me debug! ********************************* Instructions for program: You’ll use these functions to put together a program that does the following: Gives the user sentences to type, until they type DONE and then the test is over. Counts the number of seconds from when the user begins to when the test is over. Counts and reports: The total number of words the user typed, and how many...

  • Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write...

    Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write the solution to a file named p2.py and upload it here. Comprehensions will show up at the midterm exam, so it's important to practice. a) Write a list comprehension that returns a list [0, 1,4, 9, 16, .. 625] starting from range(0, 26). (lots of list elements were omitted in ...) b) Write a list comprehension that returns the list...

  • In this assignment, you will explore more on text analysis and an elementary version of sentiment...

    In this assignment, you will explore more on text analysis and an elementary version of sentiment analysis. Sentiment analysis is the process of using a computer program to identify and categorise opinions in a piece of text in order to determine the writer’s attitude towards a particular topic (e.g., news, product, service etc.). The sentiment can be expressed as positive, negative or neutral. Create a Python file called a5.py that will perform text analysis on some text files. You can...

  • I would like some assistance correcting an issue I am having with this assignment. Once a...

    I would like some assistance correcting an issue I am having with this assignment. Once a finite state automaton (FSA) is designed, its transition diagram can be translated in a straightforward manner into program code. However, this translation process is considerably tedious if the FSA is large and troublesome if the design is modified. The reason is that the transition information and mechanism are combined in the translation. To do it differently, we can design a general data structure such...

  • In python please 6 annoying_int_sequence(n) In annoying_recursion.py, write the function annoying_int_sequence(n) which takes a single integer...

    In python please 6 annoying_int_sequence(n) In annoying_recursion.py, write the function annoying_int_sequence(n) which takes a single integer parameter (which must be non-negative). It must return a list of intgers. The contents of the integers are defined recursively. Basically, each version of this sequence is made up of the next-smaller one, repeated n times - and with the number n in-between. For instance, the sequence for n = 3 is: ???? 3 ???? 3 ???? Just drop in the the sequence for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT