0% found this document useful (0 votes)
3 views6 pages

Programming Examples 1

The document provides explanations and code snippets for various programming concepts, including finding the GCD using recursion, transposing a square matrix, calculating the sum of even and odd digits in a list, and checking if a string is a palindrome. Each section includes example code and a brief description of the logic behind the implementation. The content appears to be a mix of programming instructions and examples, possibly intended for educational purposes.

Uploaded by

tis.dh11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Programming Examples 1

The document provides explanations and code snippets for various programming concepts, including finding the GCD using recursion, transposing a square matrix, calculating the sum of even and odd digits in a list, and checking if a string is a palindrome. Each section includes example code and a brief description of the logic behind the implementation. The content appears to be a mix of programming instructions and examples, possibly intended for educational purposes.

Uploaded by

tis.dh11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

programmingf-xamf.

be#aef →
defining fee a neotwo
god -7 name
of function
G#
.

1. input of
aio → fire function .

6% a → Remainder
def gcd(a, b):
if a == 0 : when 6 is divided
return b
by a .

return gcd(b%a, a)

find GCD of two


How we
normally
numbers manually :

25,35

25335 ( I 1) 25
§
-25 2÷
Remainder → To

5) so ¢
÷
So ,
GCD = 5

so when we don't find any remainder


and the last divisor is GCD
stop
.

we
recursion

similarly
here we are
using
to find GCD .

number
dividing larger
the
Each
step we are

the
with the smaller number,
collecting
remainder
remainder and passing the

smaller number
to next step for
and the

division .

return
when remainder = 0
,
we simply
the GCD
of last step as
.

the divisor

'

How the recursion works :

(25,35) Remainder
-10
GCD

GCD (10,25 ) Remainder -5


-

0
(5,103 Remainder =

GCD

Doesn't check
GCD (0,5) for remainder

a.)
N=len(A)

def alpha(A):

for i in range(N): row

for j in range(i+1, N): column .

A[i][j], A[j][i] = A[j][i], A[i][j]

is square matrix What does


A
.

this function alpha do ?

÷÷¥÷÷÷
For an
element

Ciii ) We are
swapping
it with
(ji )

so ,
in
place Transpose of . a

matrix
i=É .

I = 1 to N [1/213]
i=
[ ] for
'
N
j=i -11 to 2,3
[ a. 6
,
c ;D]
odd_sum = 0
even_sum = 0

for sub in test_list:


for ele in str(sub):
if int(ele) % 2 == 0:
-

even_sum += int(ele) → sum of all the even


in
else: digits present
odd_sum += int(ele) test list
the numbers of -

Test -
list → list
of integers

a- a -16
test -
list [35,24/143]

test list :
a -1=6 for sub in -

in Straub) :
for ele

3%2=-0
'

3 ele -53
/ '

566--35-5
Eszu
ele -55
'
2
'

'
ele =

143 -
'
'

ele 4
f
" -_
'

ele il
3 '

ele → y
'

ele =-3
1,213141T
4) intf.no ,

def beta(str): Stol 3

whether
# Run loop from 0 to len/2 Checking
for i in range(0, len(str)//2): Str is
palindrome
- if str[i] != str[len(str)-i-1]: or not .

return False
-

→ Str >
string
-

- -

else
:retvrn Tries her Gtr) /12--2
0,1
4. return True
ÑADAm
☒ (0/2)
MOM DOOD
0¥ >a ← ( 0,2)
I :O stop] != Str [5-0-1] a-
Staci g✓
M
M
B0TA⑥µ
IT
stray ! = Str / 5-1-1] = Stats] t☐☒p☒B
A
A

012
31/2=1
BAT lenfstr) =3

G. 1)
staff ! -_
stay -

oifstrt) i=o⑧

B F- range

from 1 to N:

0 I
>
2
,
3,4 I 5
, ,

' '

Mother
[ 9,6I > id
2
]
3
012345

0
range [ 0,447
psaHrar.ge/ton;0-4-#I

?
:

You might also like