
In this post, we have provided solutions to python Problem Set 1 of Introduction to Computer Science & Programming Using Python of Edx Course.
Note: Use the solution for your reference only. During the final submission, please use your own code. Thank You 🙂
Introduction to Computer Science & Programming Using Python Problem Set 1:
Problem 1 Description:
Assume s
is a string of lower case characters.
Write a program that counts up the number of vowels contained in the string s
. Valid vowels are: ‘a
‘, ‘e
‘, ‘i
‘, ‘o
‘, and ‘u
‘. For example, if s = 'azcbobobegghakl'
, your program should print:
Number of vowels: 5
The solution to Problem 1:
count = 0
vowels = [‘a’ , ‘e’ , ‘i’ ,’o’ , ‘u’]
for char in s:
if char in vowels:
count += 1
print (‘Number of vowels: ‘ + str(count))
Problem 2 Description:
Assume s
is a string of lower case characters.
Write a program that prints the number of times the string 'bob'
occurs in s
. For example, if s = 'azcbobobegghakl'
, then your program should print
Number of times bob occurs is: 2
Problem 2 Solution:
Count = 0
for i in range(len(s)):
if (s[i:i+3] == “bob”):
Count = Count + 1
print(“Number of times bob occurs is: ” + str(Count))
Problem 3 Description:
Assume s
is a string of lower case characters.
Write a program that prints the longest substring of s
in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl'
, then your program should print
Longest substring in alphabetical order is: beggh
In the case of ties, print the first substring. For example, if s = 'abcbcd'
, then your program should print
Longest substring in alphabetical order is: abc
Note: This problem may be challenging. We encourage you to work smart. If you’ve spent more than a few hours on this problem, we suggest that you move on to a different part of the course. If you have time, come back to this problem after you’ve had a break and cleared your head.
Problem 3 Solution:
first=0
second=1
strang1=”
strang2=”
while second<len(s):
if second>=len(s):
break
elif s[first]<=s[second]:
strang1+=s[first:second+1]
while second<=len(s):
second+=1
if second>=len(s):
break
elif s[second]>=strang1[-1]:
strang1+=s[second]
if len(strang1)>len(strang2):
strang2=strang1
else:
if len(strang1)>len(strang2):
strang2=strang1
strang1=”
first=second-1
break
else:
if len(s[first])>len(strang2):
strang2=s[first]
first+=1
second+=1
print(“Longest substring in alphabetical order is:” + strang2)
TOP COMPANIES HIRING DRIVE | BATCHWISE HIRING DRIVES |
1. TCS OFF-CAMPUS DRIVES | 1. 2018 BATCH |
2. INFOSYS OFF-CAMPUS DRIVES | 2. 2019 BATCH |
3. COGNIZANT OFF-CAMPUS DRIVES | 3. 2020 BATCH |
4. WIPRO OFF-CAMPUS DRIVES | 4. 2021 BATCH |
5. CAPGEMINI OFF-CAMPUS DRIVES | 5. 2022 BATCH |
To Stay Up-to-date, Follow us on Social Media:
1. Join our Telegram Channel: | Click Here |
2. Like us on Facebook: | Click Here |
3. Follow us on Instagram: | Click Here |
4. Follow us on LinkedIn: | Click Here |
5. Follow us on Google News: | Click Here |