+ 5

<Challenge> "[🔡🏆] From a long text input, build a list of all the "n" characters chains in the text" </Challenge

The user is asked for the number "n" of letters. You can use a given source text in your code, but the idea in the end is that it would be an input and can be a long one. The code has to create a list of all the distinct existing chain of "n" characters in the source text (including whitespaces, ponctuation, numbers, ...]. ex 1 : text : "spameggspam" , n=0 output has to be : [] ex 2 : text : "spameggspam" , n=3 output has to be : ['spa', 'pam', 'ame', 'meg', 'egg', 'ggs', 'gsp'] ex 3 : text : "spameggspam" , n=4 output has to be : ['spam', 'pame', 'ameg', 'megg', 'eggs', 'ggsp', 'gspa'] ex 4 : text : "spameggspam" , n=2 output has to be : ['sp', 'pa', 'am', 'me', 'eg', 'gg', 'gs'] ex 5 : text : "now you got the idea", n=1 output has to be : ['n', 'o', 'w', ' ', 'y', 'u', 'g', 't', 'h', 'e', 'i', 'd', 'a'] Efficiency of the code is the most important aspect to care for. Not to be the shortest in line count, but the lightest to process. As I am a Python player, I deeply encourage whoever can to try it with Python, so that I can read your code ! Enjoy !

16th Oct 2017, 12:32 PM
Cépagrave
Cépagrave - avatar
19 Réponses
16th Oct 2017, 1:37 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 9
https://code.sololearn.com/cOe35dLnEpH6/?ref=app
16th Oct 2017, 12:18 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 9
The logic of the first lines is to make the code work even if the user does not input a valid string and/or n integer. You can specify both, but if you fail to do it right, the code assumes something in order to be able to carry on, instead of just raising an error.
16th Oct 2017, 1:12 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 8
@Cpasgrave It should work every time if the provided input is valid. In Sololearn, you have to enter the string in the first line, hit Enter and enter the integer n in the second line. So if you enter: blablablabla 4 The output will be: ['blab', 'labl', 'abla',....,'abla']
16th Oct 2017, 1:54 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 7
Python only. Why no ❤ for other languages.. 😢
16th Oct 2017, 11:23 AM
Zephyr Koo
Zephyr Koo - avatar
+ 7
@Cpasgrave Thank you for allowing us to join the party! 🎉🎊🎉 You may highlight in the description to encourage more Python submissions too. 😉👌
16th Oct 2017, 12:30 PM
Zephyr Koo
Zephyr Koo - avatar
+ 7
Here's my C# implementation! ✌ LINQ One-Liner〰 Enumerable.Range(0, str.Length - length + 1) .Select(i => str.Substring(i, length)) .Distinct() Sorry for late to the party and unfortunately this is not in Python. Anyway I hope someone will enjoy reading my LINQ solution~ 😉 https://code.sololearn.com/coPnnnnrnO4U/?ref=app
16th Oct 2017, 2:17 PM
Zephyr Koo
Zephyr Koo - avatar
+ 7
@Capsgrave Check it out now, then ;)
16th Oct 2017, 2:24 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
@Cpasgrave It's ok, thank you! 😄
17th Oct 2017, 1:31 AM
Zephyr Koo
Zephyr Koo - avatar
+ 4
Thanks @Cpasgrave
16th Oct 2017, 1:48 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 3
@ Kuba Siekierzynski Your code is not far from the goal. Actually the last line looks good to me, I just don't understand the logic of your first lines, and in the end it doesn't make the job. Your code is asking for n as input and uses only 3 as a value, for example. Maybe some modifications ? Thanks for participating !
16th Oct 2017, 12:36 PM
Cépagrave
Cépagrave - avatar
+ 3
@ Kartikey Sahu Nice code ! Works nicely !
16th Oct 2017, 1:47 PM
Cépagrave
Cépagrave - avatar
+ 3
@ Kartikey Sahu I was about to tell you about this case when n>len(text) Good correction !
16th Oct 2017, 2:05 PM
Cépagrave
Cépagrave - avatar
+ 3
@ Kuba Siekierzyński You're right ! Sorry, I understand your code better now. You just secured it for bad input situations ... Then it works ! Only : there are still those repeated values in the output, not a big deal but it was part of the challenge.
16th Oct 2017, 2:16 PM
Cépagrave
Cépagrave - avatar
+ 3
@ Kuba Siekierzyński Nice and working ! I liked your solution to make a list from a dictionnary from a generator ! I would have used set(list) and it seems to be equivalent. Thank you, well done !
16th Oct 2017, 2:30 PM
Cépagrave
Cépagrave - avatar
+ 3
@Zephyr Koo It looks ok, the code is clear and well organised, but I can't understand it yet ... Well done, thank you !
16th Oct 2017, 8:22 PM
Cépagrave
Cépagrave - avatar
+ 2
Ok, Zephyr, you're right ! Why not other languages ? I am interested in Python because that's the only language I can read for now. But I'll edit the challenge and open the doors : ) Welcome to other languages !
16th Oct 2017, 12:16 PM
Cépagrave
Cépagrave - avatar
+ 2
@ Kuba Siekierzyński I see your idea. But still, it doesn't make it. Try 0, 1, 4, or a letter as inut, it always give a result for n=3. And also, some strings are coming twice ... keep on coding ! : )
16th Oct 2017, 1:31 PM
Cépagrave
Cépagrave - avatar
0
I will try with cpp
16th Oct 2017, 2:28 PM
Sayantan Sinharay
Sayantan Sinharay - avatar