Slice :Data Processing and Visulisation with Python (Python Exercise 11)

Data Processing and Visulisation with Python

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'))

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值