top of page
Prostate Gland releases PSA molecules (15).png

STRINGS AND LISTS

Problem:

Given:  A string s of length at most 200 letters and four integers aband d.

​

Return: The slice of this string from indices a through b and c through d (with space in between), inclusively. In other words, we should include elements s[b] and s[d] in our slice.

​

Sample Dataset: 

​

HumptyDumptysatonawallHumptyDumpthadagreatfallAlltheKingshorsesandalltheKingsmenCouldntputHumptyDumptyinhisplaceagain.

​

22  27  97  102

                            

Sample Output:  Humpty Dumpty

Solution:

A fairly simple slicing code needs to be run through the Humpty Dumpty string. It is key to keep couple a things  in mind:

​

1. Python assigns the first character of a string with the index 0, the second with the index 1 and so on...

2. The string needs to be sliced through the first two numbers and then through the last two numbers

3. s[22:27] discounts the 27th character, so the index where the slicing stops needs to be incremented by one

a     l     l     h     u     m     p     t     y     d     u     m     p     t     y     h     a     d

22        23        24         25        26       27        28         29         ...          ...         ...        ...         ...         ...         ...

...        ...       ...

a

b

< >

text= "HumptyDumptysatonawallHumptyDumptyhadagreatfallAlltheKingshorsesandalltheKingsmenCouldntputHumptyDumptyinhisplaceagain."

​

a = 22

b = 27

c = 97

d = 102

​

print(text[a:b], text[c:d])

Output:

Humpt Dumpt

That doesn't seem right...​

​

Oh we forgot to increment a one to the ending splice indexes!

< >

print(text[a:b+1], text[c:d+1])

Output:

Humpty Dumpty

That we go!

​

Copy and paste the output you get from running your code on the sample data onto the Rosalind answer terminal and submit. Annnd get yourself a cookie!

Subscribe to my mailing list and never miss a blogpost or new puzzle!

  • White Instagram Icon

Thanks for submitting!

bottom of page