SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/usr/bin/env python | |
| 2 | # -*- coding: utf-8 -*- | |
| 3 | ||
| 4 | # http://www.ling.gu.se/~lager/python_exercises.html | |
| 5 | # Wroten by mohamedAziz | |
| 6 | #----------------------------------- | |
| 7 | # ex 1 | |
| 8 | def max(nb1, nb2) : | |
| 9 | if nb1 > nb2 : | |
| 10 | return nb1 | |
| 11 | elif nb2 > nb1 : | |
| 12 | return nb2 | |
| 13 | else: return nb1 | |
| 14 | #print max(46, 80) | |
| 15 | #----------------------------------- | |
| 16 | # ex 2 | |
| 17 | def max_of_three(nb1, nb2, nb3) : | |
| 18 | a = max(nb1, nb2) | |
| 19 | b = max(a, nb3) | |
| 20 | return max(a, b) | |
| 21 | #print(max_of_three(33, 46, 12)) | |
| 22 | #----------------------------------- | |
| 23 | # ex 3 | |
| 24 | def leng(s): | |
| 25 | count = 0 | |
| 26 | for c in s: | |
| 27 | count += 1 | |
| 28 | return count | |
| 29 | #print(leng(['mohamed', 'ali'])) | |
| 30 | #----------------------------------- | |
| 31 | # ex 4 | |
| 32 | def vowel(ch): | |
| 33 | if ch.upper() in ['A', 'E', 'I', 'O', 'U', 'Y'] : | |
| 34 | return True | |
| 35 | else: | |
| 36 | return False | |
| 37 | #print(vowel('A'))
| |
| 38 | #----------------------------------- | |
| 39 | # ex 5 | |
| 40 | # the definition of consonant -> http://en.wikipedia.org/wiki/Consonant | |
| 41 | # B, C, D, F, G, H, J, K, L, M, N, P, R, S, T, V, X, Z, and usually W and Y | |
| 42 | def translate(s): | |
| 43 | phrase = '' | |
| 44 | for ch in s : | |
| 45 | if ch.upper() in ['B', 'C', 'D', 'F', 'G', 'H', 'J', | |
| 46 | 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'V', 'X', 'Z', 'W', 'Y'] : | |
| 47 | ch = ch + 'o' + ch | |
| 48 | phrase += ch | |
| 49 | return phrase | |
| 50 | #print(translate("this is fun"))
| |
| 51 | #----------------------------------- | |
| 52 | # ex 6 | |
| 53 | def sum(l): | |
| 54 | sm = 0 | |
| 55 | for nb in l : | |
| 56 | sm += nb | |
| 57 | return sm | |
| 58 | ||
| 59 | def multiply(l): | |
| 60 | mu = 1 | |
| 61 | for nb in l : | |
| 62 | mu *= nb | |
| 63 | return mu | |
| 64 | #print(sum([1, 2, 3, 4])) | |
| 65 | #print(multiply([1, 2, 3, 4])) | |
| 66 | #----------------------------------- | |
| 67 | # ex 7 | |
| 68 | def reverse(s): | |
| 69 | reversed1 = '' | |
| 70 | for ch in s : | |
| 71 | reversed1 = ch + reversed1 | |
| 72 | return reversed1 | |
| 73 | #print(reverse('mohamed'))
| |
| 74 | #----------------------------------- | |
| 75 | # ex 8 | |
| 76 | def is_palindrome(s): | |
| 77 | if reverse(s) == s : | |
| 78 | return True | |
| 79 | else : | |
| 80 | return False | |
| 81 | #print(is_palindrome('radar'))
| |
| 82 | #----------------------------------- | |
| 83 | # ex 9 | |
| 84 | def is_member(x, a): | |
| 85 | rep = False | |
| 86 | for xx in a : | |
| 87 | if xx == x : | |
| 88 | rep = True | |
| 89 | if rep == True : | |
| 90 | return True | |
| 91 | else : | |
| 92 | return False | |
| 93 | #print(is_member(6, [4, 6, 8])) | |
| 94 | #----------------------------------- | |
| 95 | # ex 10 | |
| 96 | def overlapping(l1, l2) : | |
| 97 | for x1 in l1 : | |
| 98 | if is_member(x1, l2) == True : | |
| 99 | return True; break | |
| 100 | else : | |
| 101 | return False | |
| 102 | #print(overlapping(['mohamed', 'aziz', 'ali'], ['omar', 'mohamed'])) | |
| 103 | #----------------------------------- | |
| 104 | # ex 11 | |
| 105 | def generate_n_chars(nb, x) : | |
| 106 | ret = '' | |
| 107 | for t in range(nb) : | |
| 108 | ret += x | |
| 109 | return ret | |
| 110 | #print(generate_n_chars(5, 'x')) | |
| 111 | #----------------------------------- | |
| 112 | # ex 12 | |
| 113 | def histogram(l) : | |
| 114 | for h in l : | |
| 115 | print(generate_n_chars(h, '*')) | |
| 116 | #histogram([4, 9, 7]) | |
| 117 | #----------------------------------- | |
| 118 | # ex 13 | |
| 119 | def max_in_list(l) : | |
| 120 | maxi = l[0] | |
| 121 | for nb in l : | |
| 122 | if nb > maxi : | |
| 123 | maxi = nb | |
| 124 | return maxi | |
| 125 | #print(max_in_list([33, 12, 46])) | |
| 126 | #----------------------------------- | |
| 127 | # ex 14 | |
| 128 | def word2integer(lw) : | |
| 129 | li = [] | |
| 130 | for w in lw : | |
| 131 | li.append(leng(w)) | |
| 132 | return li | |
| 133 | #print(word2integer(['mohamed', 'ali', 'omar'])) | |
| 134 | #----------------------------------- | |
| 135 | # ex 15 | |
| 136 | def find_longest_word(l) : | |
| 137 | maxi = leng(l[0]) | |
| 138 | for w in l : | |
| 139 | if leng(w) > maxi : | |
| 140 | maxi = leng(w) | |
| 141 | return maxi | |
| 142 | #print(find_longest_word(['ali', 'omar', 'mohamed'])) | |
| 143 | #----------------------------------- | |
| 144 | # ex 16 | |
| 145 | def filter_long_words(lw, n) : | |
| 146 | lwn =[] | |
| 147 | for w in lw : | |
| 148 | if leng(w) > n : | |
| 149 | lwn.append(w) | |
| 150 | return lwn | |
| 151 | #print(filter_long_words(['ali', 'omar', 'mohamed'], 4)) | |
| 152 | #----------------------------------- | |
| 153 | # ex 18 | |
| 154 | # continuez a ecrire | |
| 155 | def pangram(se) : | |
| 156 | import string | |
| 157 | bb = '' | |
| 158 | maxi = ord[se[0].lower()] | |
| 159 | for x in se : | |
| 160 | if 97 <= ord(x.lower()) <= 122 : | |
| 161 | if maxi > ord(x.lower()): | |
| 162 | pass | |
| 163 | ||
| 164 | #----------------------------------- | |
| 165 | # ex 19 |