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 .

27th Jan 2019, 3:20 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
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!
27th Jan 2019, 6:06 PM
HonFu
HonFu - avatar
+ 1
Thank you so much ^^
27th Jan 2019, 5:37 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
+ 1
Thank you HonFu you made my day !!!
27th Jan 2019, 6:05 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
+ 1
Thank you Master 🙏🙏🙏🙏🙏🙏
27th Jan 2019, 6:07 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
+ 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 :)
27th Jan 2019, 6:14 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
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.)
27th Jan 2019, 4:33 PM
HonFu
HonFu - avatar
0
No it's supposed to be a map or reduce otherwise its easy but thx anyway
27th Jan 2019, 4:34 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
0
But with map you will need some function.
27th Jan 2019, 4:48 PM
HonFu
HonFu - avatar
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)
27th Jan 2019, 4:51 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
0
It's more easier to write
27th Jan 2019, 4:51 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
0
You can use any function though, not only lambda. Is lambda a condition?
27th Jan 2019, 4:52 PM
HonFu
HonFu - avatar
0
Here is what i mean by lambda
27th Jan 2019, 4:54 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
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'))
27th Jan 2019, 4:57 PM
HonFu
HonFu - avatar
0
Oh okay i see , but how to fix this problem without using a loop or while ,only a map ? :/
27th Jan 2019, 4:58 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
0
You could try to design a recursive function. (Although there may be a simpler way... I don't use map very often. ^^')
27th Jan 2019, 4:59 PM
HonFu
HonFu - avatar
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"]
27th Jan 2019, 5:01 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
0
A map is recursive no need to make a rec function
27th Jan 2019, 5:02 PM
Sara Hadj-Ali
Sara Hadj-Ali - avatar
0
Is it? I thought map is iterative: It applies a function step by step on an iterable.
27th Jan 2019, 5:06 PM
HonFu
HonFu - avatar
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:])))
27th Jan 2019, 5:09 PM
HonFu
HonFu - avatar