0
How to solve this
i want to write a function using only a reduce or map that makes this : >Myfunction(["a","b","c","d","e","f","g","h"]) ["abc","bcd","cde","efg","fgh"] It iterates three times ,when it arrives to the letter "g" it can't go farther since we cant concat three letters HELP PLEASE , and than you .
34 Antworten
0
Welcome. :)
Be sure not to just use the solution, but play around with it a bit so that you really understand and can use these tools yourself in the future!
+ 1
Thank you so much ^^
+ 1
Thank you HonFu you made my day !!!
+ 1
Thank you Master 🙏🙏🙏🙏🙏🙏
+ 1
I already follow u in Ur profile 🙏🙏 im new i hope I'll learn a lot from this courses here and from you too as well :)
0
Can you use normal builtin tools? If yes, I'd do it like this:
s='abcdefg'
a=[]
i=0
while i<len(s)-3:
a.append(s[i:i+3])
i += 1
print(a)
If your letters come in a list, you can ''.join what you are going to append.
(And yeah, wrap a function around it if you like/need.)
0
No it's supposed to be a map or reduce otherwise its easy but thx anyway
0
But with map you will need some function.
0
What i know is that map function works this way :
map (lambda_function ,applay_to_list)
For example : we could have to scare elements on a list we apply a map this way :
map (lambda x : x**2, l)
0
It's more easier to write
0
You can use any function though, not only lambda. Is lambda a condition?
0
Here is what i mean by lambda
0
I understand, but syntaxwise you don't need to use lambda with map, you can use normal functions and methods too, like:
print(*map(str.upper, 'abcde'))
0
Oh okay i see , but how to fix this problem without using a loop or while ,only a map ? :/
0
You could try to design a recursive function. (Although there may be a simpler way... I don't use map very often. ^^')
0
A map is like a loop and should map my input which is :
["a","b","c","d","e","f","g","h"]
And should output only this :
["abc","bcd","cde","efg","fgh"]
0
A map is recursive no need to make a rec function
0
Is it? I thought map is iterative: It applies a function step by step on an iterable.
0
Would a tiny little zip be ok?
a=['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(*map(lambda x: ''.join(x), zip(a, a[1:], a[2:])))