python:201908
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python:201908 [2019/08/23 22:51] – aistis | python:201908 [2019/08/23 22:52] (current) – aistis | ||
|---|---|---|---|
| Line 10: | Line 10: | ||
| ---- | ---- | ||
| - | In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left. | + | {{:python:pasted:20190823-225243.png}} |
| - | + | ||
| - | NOTE: String letters are case-sensitive. | + | |
| - | + | ||
| - | Input Format | + | |
| - | + | ||
| - | The first line of input contains the original string. The next line contains the substring. | + | |
| - | + | ||
| - | Constraints | + | |
| - | + | ||
| - | + | ||
| - | Each character in the string is an ascii character. | + | |
| - | + | ||
| - | Output Format | + | |
| - | + | ||
| - | Output the integer number indicating the total number of occurrences of the substring in the original string. | + | |
| - | + | ||
| - | Sample Input | + | |
| - | + | ||
| - | ABCDCDC | + | |
| - | CDC | + | |
| - | + | ||
| - | Sample Output | + | |
| - | + | ||
| - | 2 | + | |
| - | + | ||
| - | Concept | + | |
| - | + | ||
| - | Some string processing examples, such as these, might be useful. | + | |
| - | There are a couple of new concepts: | + | |
| - | In Python, the length of a string is found by the function len(s), where | + | |
| - | + | ||
| - | is the string. | + | |
| - | To traverse through the length of a string, use a for loop: | + | |
| - | + | ||
| - | for i in range(0, len(s)): | + | |
| - | print (s[i]) | + | |
| - | + | ||
| - | A range function is used to loop over some length: | + | |
| - | + | ||
| - | range (0, 5) | + | |
| - | + | ||
| - | Here, the range loops over | + | |
| - | to . is excluded. | + | |
| ---- | ---- | ||
| - | Consider a list (list = []). You can perform the following commands: | + | {{:python:pasted:20190823-225214.png}} |
| - | + | ||
| - | insert i e: Insert integer | + | |
| - | + | ||
| - | at position | + | |
| - | . | + | |
| - | print: Print the list. | + | |
| - | remove e: Delete the first occurrence of integer | + | |
| - | . | + | |
| - | append e: Insert integer | + | |
| - | + | ||
| - | at the end of the list. | + | |
| - | sort: Sort the list. | + | |
| - | pop: Pop the last element from the list. | + | |
| - | reverse: Reverse the list. | + | |
| - | + | ||
| - | Initialize your list and read in the value of | + | |
| - | followed by lines of commands where each command will be of the | + | |
| - | + | ||
| - | types listed above. Iterate through each command in order and perform the corresponding operation on your list. | + | |
| - | + | ||
| - | Input Format | + | |
| - | + | ||
| - | The first line contains an integer, | + | |
| - | , denoting the number of commands. | + | |
| - | Each line of the | + | |
| - | + | ||
| - | subsequent lines contains one of the commands described above. | + | |
| - | + | ||
| - | Constraints | + | |
| - | + | ||
| - | The elements added to the list must be integers. | + | |
| - | + | ||
| - | Output Format | + | |
| - | + | ||
| - | For each command of type print, print the list on a new line. | + | |
| - | + | ||
| - | Sample Input 0 | + | |
| - | + | ||
| - | 12 | + | |
| - | insert 0 5 | + | |
| - | insert 1 10 | + | |
| - | insert 0 6 | + | |
| - | + | ||
| - | remove 6 | + | |
| - | append 9 | + | |
| - | append 1 | + | |
| - | sort | + | |
| - | + | ||
| - | pop | + | |
| - | reverse | + | |
| - | + | ||
| - | + | ||
| - | Sample Output 0 | + | |
| - | + | ||
| - | [6, 5, 10] | + | |
| - | [1, 5, 9, 10] | + | |
| - | [9, 5, 1] | + | |
python/201908.1566589894.txt.gz · Last modified: by aistis
