0

A number and a word are provided. For example:

value = 3
word = 'NAME'

I have to loop through the word NAME and remove one letter at a time and finally display the last letter remaining. The pattern of removal is: starting from the first letter, advance through value letters in a cycle (once reach the end, start from index 0) and remove the reached letter.

For example, if the value is 3. It would count 3 starting at N and remove first M.

Remaining is NAE - count to 3 from the next letter which is now E, then A will be removed.

Remaining is NE - count to 3, starting from E then E will be removed and only N remains. The result will be shown N. I am not able to find a way to construct a loop to do this iteration.

As of now I’ve done manually and made it like

if value == 4:
  print('A')
elif value == 1:
  print('E')
elif value ==2:
  print('N')

This is not practical since the value would be anything between 1-100. Any other solution by which this will automatically iterate and remove and show me the final letter.

Zefira
  • 4,329
  • 3
  • 25
  • 31
Samad Asharaf
  • 13
  • 1
  • 3
  • i can show you another example. say the value = 5, word = 'NAME' step 1 - counting starts from N and when it reach 5 it removes that letter which is N in our case. step 2 - counting starts from A and when it reach 5 that letter is removed which is M. now remaining AE step 3 - counting starts from E and when it reach 5 that letter is removed which is E. result - A. end – Samad Asharaf Feb 18 '20 at 15:31
  • Hi Samad, welcome to Stack Overflow! this looks like it might be a homework question, and as such I would like to see a little more of an attempt at writing your own implementation. You can read more in detail about the [community guidelines on asking for help with homework questions](https://meta.stackoverflow.com/q/334822). – Zefira Feb 19 '20 at 12:48
  • A few pointers to help you out. I suggest you look into [modular arithmetic](https://medium.com/i-math/intro-to-modular-arithmetic-34ad9d4537d1) and think about how you could use a loop to keep removing items until there is only one left. [How to remove an element from a list by index](https://stackoverflow.com/a/627453/242135) will also probably be helpful. – Zefira Feb 19 '20 at 12:54
  • Geoff this is not any homework but as a part of self study i picked up it myself. this is not the whole work. actually this is just a part of the whole coding but very critical part. I've been trying to remove element from a list but not able to cycle through the word and remove. – Samad Asharaf Feb 19 '20 at 19:25

1 Answers1

1

You can't actually "remove" characters from a string as strings are immutable. What you can do, is reassign with a modified string.

As for the looping, you might use the % (modulus) operator for the cyclic effect. First of all you will need to mark the first index to count from at each iteration:

start = 0

Then we will want to keep iterating until we are left with one letter:

while len(word) > 1:

Now we want to find the index of the next letter to be removed. As I said, with the use of %, we will get back to 0 after the last letter:

    to_remove = (start + value - 1) % len(word)

Now we need to actually remove the found letter, as I explained we will have to re-assign the variable, and we will get rid of the letter by using slicing:

    word = word[:to_remove] + word[to_remove+1:]

All together we have somthing like:

def remove_letters(word, value):
    start = 0
    while len(word) > 1:
        to_remove = (start + value - 1) % len(word)
        start = to_remove
        word = word[:to_remove] + word[to_remove+1:]
    return word

A few examples:

>>> remove_letters('NAME', 3)
'N'
>>> remove_letters('NAME', 4)
'A'
>>> remove_letters('NAME', 5)
'A'
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61