Data Processing and Visulisation with Python
Python Exercise 11
Repeated or not
Write a Python function to test if a given non-empty string has any character repeated. If there is no duplication of character in string, then return False, otherwise return True
method 1
def isCharRepeated(m):
for i in m :
if m.count(i) > 1 :
return True
else :
return False
print(isCharRepeated('Zhongnan University'))
print(isCharRepeated('Wuhan Bridge'))
method 2
def isCharRepeated(s):
for i in s:
counts=s.count(i)
if counts>1:
return True
return False
print(isCharRepeated('Zhongnan University'))
print(isCharRepeated('Wuhan Bridge'))
method 3
def isCharRepeated(s):
for i ,c in enumerate(s[:-1]):
if s[i+1:].find(c)>=0:
return True
return False
print(isCharRepeated('Zhongnan University'))
print(isCharRepeated('Wuhan Bridge'))
Digital or not
Write a Python function to test if a given non-empty string contains no characters other than digits.
method 1
def isDigit(m):
for i in m :
if ord(i) >=97 and ord(i)<=122:
return False
else :
return True
print(isDigit('4326253'))
print(isDigit('345d57'))
method 2
def isDigit(s):
for i in s:
if ord(i) in range(48,58):
a = 1
else:
a = 0
break
if a==1:
return True
if a==0:
return False
print(isDigit('4326253'))
print(isDigit('345d57'))
method 3
def isDigit(s):
for i in s:
if ord(i) not in range(48,58):
return False
return True
print(isDigit('4326253'))
print(isDigit('345d57'))
method 4
def isDigit(s):
return s.isdigit()
print(isDigit('4326253'))
print(isDigit('345d57'))
Alphabet or not
Write a Python function to test if a given non-empty string contains only alphabets or not.
method 1
def isAlphabet(m):
for i in m :
if ord(i) >=48 and ord(i)<=57:
return False
else :
return True
print(isAlphabet('asiuIOUGIOUGasr'))
print(isAlphabet('as2345lkjhl'))
method 2
def isAlphabet(m):
for i in m :
if (ord(i) not in range(65,91)) and (ord(i) not in range(97,123)):
return False
return True
print(isAlphabet('asiuIOUGIOUGasr'))
print(isAlphabet('as2345lkjhl'))
method 3
def isAlphabet(m):
for c in m :
if not c.isalpha():
return False
return True
print(isAlphabet('asiuIOUGIOUGasr'))
print(isAlphabet('as2345lkjhl'))
method 4
def isAlphabet(m):
return m.isalpha()
print(isAlphabet('asiuIOUGIOUGasr'))
print(isAlphabet('as2345lkjhl'))
Seperator
Write a Python function to test if a given non-empty string contains only digits, if yes, then format the string with thousand seperators in proper position, then return the result, otherwise return the original string.
Hint:
You can use your previous functions.
method 1
def addSeperator(m):
if isDigit(m) == True :
return format(int(m) , ',')
else:
return m
print(addSeperator('2436562457456'))
print(addSeperator('45'))
print(addSeperator('346534sd'))
method 2
def addSeperator(s):
if isDigit(s)==True:
x='{:,}'.format(int(s))
return x
return s
print(addSeperator('2436562457456'))
print(addSeperator('45'))
print(addSeperator('346534sd'))
method 3
def addSeperator(s):
if s.isdigit():
n = len(s)
t = n % 3
r = s[:t]
for i in range(n//3):
if r != '':
r += ','
r += s[t+i*3:t+i*3+3]
return r
return s
print(addSeperator('2436562457456'))
print(addSeperator('45'))
print(addSeperator('346534sd'))
method 4
def addSeperator(s):
if s.isdigit():
return f'{int(s):,}'
return s
print(addSeperator('2436562457456'))
print(addSeperator('45'))
print(addSeperator('346534sd'))
Palindrome or not
Write a Python function to test if a given non-empty string is a palindrome.
method 1
def isPalindrome(m):
from math import ceil
n=str(m)
k=len(n)
for i in range(ceil(k/2)):
if n[i] != n[-1-i]:
return False
else:
return True
print(isPalindrome('madam'))
print(isPalindrome('tap non pat'))
print(isPalindrome('welcome'))
print(isPalindrome('tap nom pat'))
method 2
def isPalindrome(str2):
n = len(str2)
for i in range(0,n//2):
if str2[i] != str2[-1-i]:
return False
return True
print(isPalindrome('madam'))
print(isPalindrome('tap non pat'))
print(isPalindrome('welcome'))
print(isPalindrome('tap nom pat'))
Sum of digits
Write a Python function to compute sum of digits of a given string.
def sumDigits(m):
sum = 0
for i in m :
if ord(i) >=48 and ord(i)<=57:
sum = sum + int(i)
return sum
print(sumDigits('iug2345k'))
print(sumDigits('sagserg'))
Number extraction
Write a Python function to filter out all non-digit characters in a gien string, and return the extracted number in integer, if there is no digit in the string, return 0.
method 1
def numberExtract(m):
count = 0
new_m = ''
for i in m :
if ord(i) >=48 and ord(i)<=57:
new_m = new_m + i
count = count + 1
if count >0:
return new_m
else :
return 0
print(numberExtract('32lkujh43kj5kj7'))
print(numberExtract('asdgds'))
method 2
def numberExtract(s):
new_s=''
for i in s:
if 48<=ord(i)<=57:
new_s+=i
return int(new_s) if new_s!='' else 0
print(numberExtract('32lkujh43kj5kj7'))
print(numberExtract('asdgds'))
Permutation *
Write a Python function showPermutation(s)
to check if s
contains non-duplicated alphabets, if so, print out all the permutations of these alphabets, otherwise do nothing.
Hint:
Recursion may help.
method 1
def search(s,s1,length):
for i in s:
s2=s1+i
if isCharRepeated(s2):continue
if length==len(s):print(s2)
else:search(s,s2,length+1)
def showPermutation(s):
if (isAlphabet(s))and(not isCharRepeated(s)): search(s,'',1)
method 2
def permute(p,s):
if len(s):
for i , c in enumerate(s):
permute(p+c,s[:i]+s[i+1:])
else:
print(p)
def showPermutation(s):
if isAlphabet(s) and not isCharRepeated(s):
permute('',s)
print(showPermutation('ABC'))