0

I have a problem in Python that I don't really understand what is required, please help me.

The problem is this:

A string is given. The string to be divided into fragments of three consecutive symbols. In each fragment the middle symbol is replaced by a random character that does not coincide with any of the characters in this fragment. The fragments should be sorted alphabetically.

All I did was just this code:

string = "Geeksforgeek"

n = 3

out = [(string[i:i+n]) for i in range(0, len(string), n)]
print(out)



otoo
  • 21
  • 6
  • 1
    The problem consists of multiple steps. Did you understand that there are multiple steps? Did you try to solve each step one at a time? What was unclear to you at each step? – mkrieger1 Nov 11 '21 at 17:56
  • Break the task into sub tasks. 1) The string to be divided into fragments of three consecutive symbols 2) In each fragment the middle symbol is replaced by a random character that does not coincide with any of the characters in this fragment 3) The fragments should be sorted alphabetically. – balderman Nov 11 '21 at 17:56
  • I don't really understand what is required, I just know that I have to align them 3 by 3 and then ??? but I didn't quite succeed either – otoo Nov 11 '21 at 17:58
  • What do you think should be the result of the first step "divide into fragments of 3 consecutive symbols", given the input "geeksforgeeks"? – mkrieger1 Nov 11 '21 at 17:59
  • I think it should look "gee ksf org eek" ok and last I can delete it from the string because it shouldn't be 2 anymore – otoo Nov 11 '21 at 18:03
  • should be 2 more – otoo Nov 11 '21 at 18:04
  • Great! Now what do you think should (or could) be the result of the next step "in each fragment the middle symbol is replaced by a random character"? – mkrieger1 Nov 11 '21 at 18:05

3 Answers3

1

Here is a possible approach, see comments inside the code:

import random

s = "Geeksforgeek"

letters = set('abcdefghijklmnopqrstuvwxyz')

out = []
for i in range(0, len(s), 3):
    # split 3 by 3
    sub = s[i:i+3]
    # replace middle letter
    if len(sub)>1:
        a,_,c = sub
        # with random letter not
        # in initial sub
        b = random.choice(list(letters.difference(sub)))
        sub = a+b+c
    out.append(sub)

# sort list
sorted(out)

Output: ['edk', 'gke', 'kof', 'ong', 's']

mozway
  • 194,879
  • 13
  • 39
  • 75
  • thanks for the code but it doesn't work – otoo Nov 12 '21 at 09:31
  • @otoo how does it not work? Have you initialized `s = "Geeksforgeek"`? – mozway Nov 12 '21 at 09:33
  • yes I did but I don't get the result I get a message like this Try the new cross-platform PowerShell https://aka.ms/pscore6 – otoo Nov 12 '21 at 09:40
  • so no error name shows, but I don't see the result I don't know why and when I run it only opens that message – otoo Nov 12 '21 at 09:42
  • Or, if you run as a script you need to print: `print(sorted(out))` – mozway Nov 12 '21 at 09:43
  • aaa ok I understand I will try to solve but your answer seems correct – otoo Nov 12 '21 at 09:45
  • yes I put print to sorted and I get the result but the last step is to display them in alphabetical order I don't quite understand what they have to do, that is, after changing the one in the middle to arrange them alphabetically or how – otoo Nov 12 '21 at 09:50
  • if the result is so ['edk', 'gke', 'kof', 'ong', 's'] after the alphabetical alignment nar should look like this ['dek', 'egk', 'fko', 'gno', 's'] – otoo Nov 12 '21 at 09:55
  • That wasn't my understanding of the assignment ;) Anyway this is simple enough to do. Do you want to give it a try? – mozway Nov 12 '21 at 09:59
  • well yes if I could I could try – otoo Nov 12 '21 at 10:03
0

You are supposed to break the string into fragments of 3 each and then in each fragment, you are supposed to replace the middle character with a random character which is not one of the other 2 character in the fragment and then sort the fragment alphabetically.

  • @otoo An approach to doing this could be to iterate through all the letters in the string and append 3 letters into a string each time and add this string to a list. Then iterate over all the elements of this list and replace the middle character and add all the elements into one string and sort the string. As far as not knowing what to do , you can search for keywords like "iteration in python", "sorting in python" , "string manipulation in python" and learn the details yourself. – Extorc Productions Nov 11 '21 at 18:09
  • you sound like my teacher :) – otoo Nov 11 '21 at 18:13
0

Break the problem down into smaller parts

A string is given.

# Write some code to assign a string to a variable
s = ...

The string to be divided into fragments of three consecutive symbols.

# Break the string into lists of length 3
lists_of_strings = # something

In each fragment the middle symbol is replaced by a random character that does not coincide with any of the characters in this fragment.

# For each list of three strings, replace the middle character with a random character that is not already in this list

for l in lists_of_strings:
    # get strings in list
    # generate random character not in this list
    # replace the middle character with this character

The fragments should be sorted alphabetically.

# Sort each list alphabetically
for l in lists_of_strings:
    # sort string alphabetically
Tom McLean
  • 5,583
  • 1
  • 11
  • 36
  • the first step I did but in the second step what to do – otoo Nov 11 '21 at 18:07
  • 1
    A key skill in computer science is being able to find out how to do things, for instance, googling "how do I split a string by length in python?" gets this stack overflow answer https://stackoverflow.com/questions/13673060/split-string-into-strings-by-length – Tom McLean Nov 11 '21 at 21:17