Sunday, August 26, 2018

FEW MORE PYTHON SAMPLE PROGRAMS

PROGRAM TO PRINT THE PATTERN OF THE INPUT GIVEN:

STATEMENT:

The number of rows N is passed as the input. The program must print the half pyramid using the numbers from 1 to N.

PROGRAM:

N = int(input())
for ctr in range(1,N+1):
    for printnum in range (1, ctr+1):
        print (printnum, end=" ")
    print ("")


Input:
5

Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

PROGRAM TO COUNT THE COMMON CHARACTERS IN TWO STRINGS:

STATEMENT:

Two string values S1 and S2 are passed as input. The program must print the count of common characters in the strings S1 and S2. Assume the alphabets in S1 and S2 will be in lower case.

PROGRAM;

str1 = input().strip()
str2 = input().strip()
uniquechars = set(str1)
commoncount = 0
for ch in uniquechars:
    if ch in str2:
        commoncount+=1

print (commoncount)

Input:
energy
every

Output:
3

Explanation:
The common characters are e,r,y

No comments:

Post a Comment