-1

I solved the logical part of the following exercise, but can't find a simple solution to output in the required format. It looks like a list within a list to me.

Problem description:

Write a program that will take a string of digits and give you all the possible consecutive slices of length n in that string.

Raise an error if n is larger than the length of the string.

Examples For example, the string "01234" has the following 2-digit slices:

[[0, 1], [1, 2], [2, 3], [3, 4]]

The same string has the following 4-digit slices:

[[0, 1, 2, 3], [1, 2, 3, 4]]

This is my code which is working fine except the way it returns the answer is different so can't submit it.

digits = "01234"
n = 4
result = [None]*(len(digits)-n+1) #create a list with required size
for x in range(0, len(digits)-n+1):
    result[x] = digits[x:n]
    n += 1
print(result)

This code prints ['0123', '1234'], which is the correct answer but I want it in this format: [[0, 1, 2, 3], [1, 2, 3, 4]]

(edit) adding this line solved my problem suggested by mkrieger1, thanks This is the final code: def series_slices(digits, n): digits = [int(x) for x in digits] result = [] for x in range(0, len(digits)-n+1): result.append(list(digits[x:n])) n += 1 return result

  • are you looking for `json.dump` https://docs.python.org/3/library/json.html – shams.kool Jun 24 '19 at 12:38
  • Welcome to Stack Overflow! Concerning your problem, at what point exactly does the value of your variables not have the desired state? In other words, extract a [mcve]! As a note, Python knows two ways to "print" an object, one is using the conversion to a string and the other is via `repr()` and each class' implementation thereof. Check the docs to understand how your code works. Also, as a new user here, take the [tour] and read [ask]. – Ulrich Eckhardt Jun 24 '19 at 12:40
  • 1
    You need to convert the input string to a list of integers at the beginning: `digits = [int(x) for x in digits]`. The rest of the code should then work unchanged. – mkrieger1 Jun 24 '19 at 12:40
  • Possible duplicate of [How to split string without spaces into list of integers in Python?](https://stackoverflow.com/questions/29409273/how-to-split-string-without-spaces-into-list-of-integers-in-python) – mkrieger1 Jun 24 '19 at 12:43
  • @mkrieger1 i am trying what you suggested, but i am thinking, converting the input string into int() would take away the leading zero in the input string. wouldn't it.? – Nouraiz Shahid Jun 24 '19 at 12:46
  • THat's not "converting the input string into int()" but into a list of integers. – Stop harming Monica Jun 24 '19 at 12:47
  • No, it wouldn't, because it doesn't convert the string to a single integer at once, but converts the individual characters. – mkrieger1 Jun 24 '19 at 12:50
  • @mkrieger1 thanks friend, digits = [int(x) for x in digits] this line solved my problem. & this inline loop syntax is cool, i need to master it – Nouraiz Shahid Jun 24 '19 at 13:47

3 Answers3

1

Just use list on your strings:

list('0123')

returns

['0', '1', '2', '3']

And convert it to integers (thanks mkrieger, I hadn't read the question carefully enough:

You can use map, that will iterate on the characters of your string and return an integer for each one, then make a list out of the map object:

list(map(int, '0123'))
# [0, 1, 2, 3]

or use a list comprehension:

[int(digit) for digit in '0123'] 
# [0, 1, 2, 3]

More generally, applying list to anything iterable will return a list of its items.

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
0

You c an do this with a list comprehension. First level goes through the sizes and second level takes the slices for that size:

digits = "01234"
r=[list(digits[start:][:size+1]) for size in range(len(digits)) for start in range(len(digits)-size)]
print(r)

# [['0'], ['1'], ['2'], ['3'], ['4'], 
#  ['0', '1'], ['1', '2'], ['2', '3'], ['3', '4'], 
#  ['0', '1', '2'], ['1', '2', '3'],['2', '3', '4'], 
#  ['0', '1', '2', '3'], ['1', '2', '3', '4'], 
#  ['0', '1', '2', '3', '4']]
Alain T.
  • 40,517
  • 4
  • 31
  • 51
-1

change your code to result = [] and result.append(list(digits[x:n])) in line 3 and line 5.

Tobias Feil
  • 2,399
  • 3
  • 25
  • 41