Learn practical skills, build real-world projects, and advance your career

Python Challenging questions

Alphabetic patterns
Description
Given a positive integer 'n' less than or equal to 26, you are required to print the below pattern

Sample Input: 5

Sample Output :

--------e--------

------e-d-e------

----e-d-c-d-e----

--e-d-c-b-c-d-e--

e-d-c-b-a-b-c-d-e

--e-d-c-b-c-d-e--

----e-d-c-d-e----

------e-d-e------

--------e--------

n=5
alpha="abcdefghijklmnopqrstuvwxyz"
s=""

l=[]

for i in range(n):
    s="-".join(alpha[i:n])

    l.append(s[::-1]+s[1:])

length=len(l[0])

for i in range(n-1,0,-1):
    print(l[i].center(length,"-"))
    
for i in range(n):
    print(l[i].center(length,"-"))    
--------e-------- ------e-d-e------ ----e-d-c-d-e---- --e-d-c-b-c-d-e-- e-d-c-b-a-b-c-d-e --e-d-c-b-c-d-e-- ----e-d-c-d-e---- ------e-d-e------ --------e--------

Sum of Primes
Description
Write python code to find the sum of prime numbers from 2 to n where n is a positive integer entered by the user.

Note: n can be non-prime or prime. You have to find sum of primes till n and not sum of n prime numbers. i.e. for input 10, output should be 17.

num=12 #int(input())


def primen(n):
    f=0    
    for i in range(2,n):
        if n%i==0:
            f=f+1
    return(f)
count=0
for i in range(2,num+1):
    if primen(i) ==0:
        count=count+i
print(count)


## write a function to find if prime or not, then call that and add if prime
28