Counting a count in Python and adding 2 numbers from a single line of input

I am following the python website for my work. This is very neat, it gives you tasks to complete and compile code in a browser. Anyway, I came up with the challenge that I'm not quite sure how to do this.

One of the questions:

The same substring can occur several times within the same string: for example, "evaluates" has the substring "sses" 2 times and the "trans-Panamanian banana" has the substring "a" 6 times. To write a program that takes two lines of input, we call the first needle and the second haystack. Print the number of times this needle is a substring of a haystack.

I'm not too sure how to start this, I know that I need to compare two lines, but how? I used the method count, but it did not recognize the second occurrence ssesin assesses.

My second question is the one that I decided, but deceived a little.

The question was:

Write a program that takes one input line of the form "number1" + "number2", where both of them are positive integers, and prints the sum of two numbers. For example, at input 5 + 12, the output should be 17.

I used the method eval()and it worked, I just think that this is not what the grader had in mind for this.

Any understanding would be greatly appreciated.

EDIT: The second question has been resolved.

+3
source share
9 answers

str.count() , . str.find() , -1, :

>>> 'assesses'.find('sses', 0)    # first look at the start of the string
1
>>> 'assesses'.find('sses', 2)    # now look at previous index + 1
4
>>> 'assesses'.find('sses', 5)    # now look at previous index + 1
-1

, -1, , 'assesses' , 'sses'.

split() '+', , int() , .

+3

, 1:

needle = input()    #defines first input string
haystack = input()    #defines second input string
times = 0     #defines number of times first input string is in second
for a in range(0, len(haystack) - 1):     #loop to check strings within haystack
    if haystack[a:len(needle) + a] == needle:     #checking for needle in haystack
        times = times + 1     #increasing our number of occurances if true
print(times)     #and here is your output
+2

:

S = input()
x = S.find('+')
print(int(S[0:x])+(int(S[x+1:]))) 
+1

, , indx, , loc, ( int) str1 str2

str1 = input() #input 1
str2 = input() #input 2
count=0  #occurrence counter
indx=0  #index of string to start search at
for i in str2:  #for character i in input 2
    loc = str2.find(str1, indx)  #starting index of occurrence
    if loc == -1:  #-1 means there were no occurences
        break  #in this case break the search
    indx=loc+1  #to find the next occurrence, the new starting point will be the starting location of the last occurrence + 1
    count=count+1  #add an occurrence
print(count) #voila!
+1
  • STRING.count . , "sses".

    sses -ses, asse- sses. ? "trans-Panamanian banana".count("an") .

  • , eval(), , . , + , . , , gpa;).

EDIT: F.G. , . !

0
  • .

  • sum (map (lambda val: int (val.strip()), INPUT.split( "+" )))

0

:

def occurs(string, sub):
count = start = 0
while True:
    start = string.find(sub, start) + 1
    if start > 0:
        count+=1
    else:
        return count

.

0

:

needle=input()
haystack=input()
counter=0
for i in range(0,len(haystack)):
  if(haystack[i:len(needle)+i]!=needle):
     continue
  counter=counter+1
print(counter)

:

S = input()
for position in range(0, len(S)):
plus=S[position]
    if (plus!="+"):
      continue
    number1=int(S[0:position])
    number2=int(S[position+1:len(S)])
    print(number1+number2)
0

I found the answer to the second question:

S=input()
x=len(S)
for i in range (0,x):
   w= S[i]
   if w == '+': 
      a=(S[0:i])
      b=(S[i+1:x])
      u=int(a)
      o=int(b)
      p=u+o
      print(p)
0
source

All Articles