0% found this document useful (0 votes)
61 views

3python Live Class Upload-1

This document provides Python code solutions to various programming problems and challenges. It includes solutions for problems such as swapping the first and last elements of a list, checking if a string is a palindrome, creating a list of tuples from a given list, and printing an inverted star pattern. It also shares links to join the Perfect Plan B Slack group and follow their social media profiles.

Uploaded by

navin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

3python Live Class Upload-1

This document provides Python code solutions to various programming problems and challenges. It includes solutions for problems such as swapping the first and last elements of a list, checking if a string is a palindrome, creating a list of tuples from a given list, and printing an inverted star pattern. It also shares links to join the Perfect Plan B Slack group and follow their social media profiles.

Uploaded by

navin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Perfect Plan B

Learn, grow and become leaders of tomorrow


Problem : Python program to interchange first
and last elements in a list

Examples:

Input : [12, 35, 9, 56, 24]

Output : [24, 35, 9, 56, 12]

Input : [1, 2, 3]

Output : [3, 2, 1]
Solution 1 : Python program to interchange first
and last elements in a list
# Python3 program to swap first
# and last element of a list

# Swap function
def swapList(newList):
size = len(newList)

# Swapping
temp = newList[0]
newList[0] = newList[size - 1]
newList[size - 1] = temp

return newList

# Driver code
newList = [12, 35, 9, 56, 24]

print(swapList(newList))
Solution 2 : Python program to interchange first
and last elements in a list
# Python3 program to swap first
# and last element of a list

# Swap function
def swapList(newList):

newList[0], newList[-1] = newList[-1], newList[0]

return newList

# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList))
Solution 3 : Python program to interchange first
and last elements in a list
# Python3 program to swap first
# and last element of a list

# Swap function
def swapList(list):

# Storing the first and last element


# as a pair in a tuple variable get
get = list[-1], list[0]

# unpacking those elements


list[0], list[-1] = get

return list

# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList))
Solution 4 : Python program to interchange first
and last elements in a list
# Python3 program to swap first
# and last element of a list

# Swap function
def swapList(list):

first = list.pop(0)
last = list.pop(-1)

list.insert(0, last)
list.append(first)

return list

# Driver code
newList = [12, 35, 9, 56, 24]

print(swapList(newList))
Problem : Python program to check if a string is
palindrome or not

Examples:

Input : malayalam
Output : Yes

Input : geeks
Output : No
Solution 1 : Python program to check if a string is
palindrome or not
# function which return reverse of a string

def isPalindrome(s):
return s == s[::-1]

# Driver code
s = "malayalam"
ans = isPalindrome(s)

if ans:
print("Yes")
else:
print("No")
Solution 2 : Python program to check if a string is
palindrome or not
# function to check string is
# palindrome or not
def isPalindrome(str):

# Run loop from 0 to len/2


for i in xrange(0, len(str)/2):
if str[i] != str[len(str)-i-1]:
return False
return True

# main function
s = "malayalam"
ans = isPalindrome(s)

if (ans):
print("Yes")
else:
print("No")
Solution 3 : Python program to check if a string is
palindrome or not
# function to check string is
# palindrome or not
def isPalindrome(s):

# Using predefined function to


# reverse to string print(s)
rev = ''.join(reversed(s))

# Checking if both string are


# equal or not
if (s == rev):
return True
return False

# main function
s = "malayalam"
ans = isPalindrome(s)
Problem : Python program to create a list of tuples from
given list having number and its cube in each tuple

Example:

Input: list = [1, 2, 3]


Output: [(1, 1), (2, 8), (3, 27)]

Input: list = [9, 5, 6]


Output: [(9, 729), (5, 125), (6,
216)]
Solution : Python program to create a list of tuples from
given list having number and its cube in each tuple
# Python program to create a list of tuples
# from given list having number and
# its cube in each tuple

# creating a list
list1 = [1, 2, 5, 6]

# using list comprehension to iterate each


# values in list and create a tuple as specified
res = [(val, pow(val, 3)) for val in list1]

# print the result


print(res)
Problem : Python | Print an Inverted Star Pattern

Examples:

Below is the inverted star pattern of size


n=5
(Because there are 5 horizontal lines
or rows consist of stars).

*****
****
***
**
*
Solution : Python | Print an Inverted Star Pattern
# python 3 code to print inverted star
# pattern

# n is the number of rows in which


# star is going to be printed.
n=11

# i is going to be enabled to
# range between n-i t 0 with a
# decrement of 1 with each iteration.
# and in print function, for each
iteration,
# ” ” is multiplied with n-i and ‘*’ is
# multiplied with i to create correct
# space before of the stars.
for i in range (n, 0, -1):
print((n-i) * ' ' + i * '*')
Slack Invite Link

https://join.slack.com/t/perfect-plan-b/sh
ared_invite/zt-drplefyv-x1vurrlFy98UOe1
irCfLXw
Social Media Links

Facebook: https://www.facebook.com/IshanPlanB/

Twitter: https://twitter.com/PerfectPlanB1

Linkedin: https://www.linkedin.com/company/perfect-plan-b/

Instagram: https://www.instagram.com/perfect_plan_b/

Quora:
https://www.quora.com/q/hreieuophqgaswqv?ch=10&share=41d2481e&srid=E
R3y0

Youtube: https://www.youtube.com/channel/UCQJFQlCdcq4XxJDqE3IqmbQ

You might also like