1

I am given a string : 'stackoverflow'. Input should be a integer, for example lets say a=4. What my code should do is, it should count length of substrings occuring at overlapping intervals of 4 that is (considering string 'stackoverflow')-

  • String one : stac
  • String two : tack
  • String three : acko
  • String four : ckov

and it goes on till the end of the string. Then later I need to find the string that occurs most number of times. So far, what I have done is -

def a_length():
    string=input('Enter string : ')
    substr=list('')
    a=input('Enter length of a : ')
    for i in range(len(string)):
        substr += string[i:i+a]
        print(substr)

What I am trying here is to get the a list of substrings present but it gives an error which I do not understand -

AttributeError: 'str' object has no attribute 'slice'

But this definately doesn't work. Any help would be appreciated. Thank you!

3
  • unrelated, but why do you initialise a list as list('') instead of either list() or []? Commented May 3, 2017 at 12:45
  • @SpoonMeiser : As of what I thought was, it is a list of strings. Thus, the list(''). In anyways it works both the ways, if you initialize it by '[]' or just by 'list()'. Commented May 3, 2017 at 12:53
  • list('') takes the string, and converts it to a list; it's an empty list simple because the string has no characters. For example list('foo') is the same as ['f', 'o', 'o']. Lists can contain different types too ([1, 'a'] is perfectly valid), so your syntax makes it look as though you've misunderstood something. Commented May 4, 2017 at 8:42

2 Answers 2

3
string=input('Enter string : ')
substr=list('')
a=int(input('Enter length of a : '))
for i in range(len(string)):
    substr.append(string[i:i+a])
print(substr)

Be careful that in this way also strings "low", "ow" and "w" are generated

If you want to stop at "flow", substitute this line in the for loop

for i in range(len(string)-a+1):

Also, with list comprehension: substr = [string[i:i+a] for i in range(len(string)-a+1)]

Sign up to request clarification or add additional context in comments.

4 Comments

This does not work, I tried this too earlier. Sorry!
Should also explain that you're passing the user input to int, as that seems to be the key difference
On my machine it works. Did you notice the int() when taking the a value from input() ?
@PMonti : Oops! I am extremely sorry, I misread your answer and it does work perfectly fine! Thank you!
0

I think you are looking for something like this

s = input('Enter string')
a = int(input('Enter the interval'))
words_count = dict()
len1 = len(s)
i = 0
while i + a < len1:
  new_word = s[i:i+a]
  words_count[new_word] = words_count.get(new_word, 0) + 1
  i = i + 1


for word in words_count:
  print(word)
  print(words_count[word])  

You can run my code here - https://repl.it/HdrX/1

Hope this help !

7 Comments

This one's a great code as well, the only thing is that I could not actually get the total of a specific occurence of a substring.
I actually cannot figure that out from PMonti 's code as well. If you can actually guide me to get that, that would be very helpful.
Hi Srk, so ya if you run my code, you will be able to get the count of each words as well, do you see the variable word_count initialised to dict(), so that is like a hash map to store the count of each word as - 'kav' -> '2' etc.
So in the for loop if you look closely this line - words_count[new_word] = words_count.get(new_word, 0) + 1 .... does the taks of calculating frequency of each word.
Yes I have exactly understood that, so what I did was took the string 'stackstackoverflow' as an input string. But even then, I get each substring number as 1. Any insights on that?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.