├── .gitignore ├── Dictionary.py ├── List.py ├── README.md ├── Strings.py ├── Tuples.py └── basic-programs.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /Dictionary.py: -------------------------------------------------------------------------------- 1 | # Python | Sort Python Dictionaries by Key or Value 2 | d = {'ravi': 10, 'rajnish': 9, 'sanjeev': 15, 'yash': 2, 'suraj': 32} 3 | l = [u for u in d] 4 | l.sort() 5 | c = {i:d[i] for i in l} 6 | print(c,"sort") 7 | 8 | 9 | # Python program to find the sum of all items in a dictionary 10 | 11 | d = {'Jeet': 10, 'rajnish': 9, 'meena': 15, 'yash': 2, 'kp': 32} 12 | 13 | print(sum(d.values())) 14 | -------------------------------------------------------------------------------- /List.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | # @@@ WELLCOME TO LIST PROGRAM @@@@@@ 4 | 5 | # List Append indexing ,slicing, iterarting..... 6 | 7 | from enum import unique 8 | 9 | 10 | List = [1,2,6,3,4] 11 | 12 | for i in List: 13 | print(i) 14 | List.append(12) 15 | List[0]=121 16 | print(List) 17 | 18 | List.pop() 19 | 20 | l1 = [1,2,23,3,9] 21 | 22 | l1 = [i for i in l1 if i>2] 23 | 24 | fruits = ["apple", "banana", "cbherry", "kiwi", "mango"] 25 | 26 | 27 | l6 = [f for f in fruits if 'b' in f] # List Comperiehence 28 | l1.sort() # Sort List 29 | print(l1) 30 | 31 | list = [9,2,33,11,22,33,2] 32 | 33 | list.sort(reverse=True) # SOrt Decending Orders 34 | 35 | list.reverse() 36 | list.extend(l1) # Extends Elements From One List to Another one 37 | print(list) 38 | 39 | 40 | # Remove Duplicate Elements From List: 41 | 42 | list = [1,2,3,2,3,1,2,4,22,121] 43 | unique_list = [] 44 | for i in list: 45 | if i not in unique_list: 46 | unique_list.append(i) 47 | print(list) 48 | print(unique_list) 49 | 50 | 51 | 52 | # Program To Sort List Elements Without using Sort functions 53 | 54 | list_data = [122,11,3434,21,656,8,3223,2] 55 | l =len(list_data) 56 | 57 | for i in range(l-1): 58 | for j in range(l-i-1): 59 | if list_data[j]>list_data[j+1]: 60 | list_data[j],list_data[j+1] = list_data[j+1],list_data[j] 61 | print(list_data,"Sort List Data") 62 | 63 | 64 | # Program to search elements in list 65 | 66 | list_data = [122,11,3434,21,656,8,3223,2] 67 | 68 | s = 11 69 | 70 | for i in list_data: 71 | if s in list_data: 72 | print("Yes Elements Presents---") 73 | else: 74 | print("Not Present Elements") 75 | 76 | 77 | 78 | # Program To Find Largest Numbers in List 79 | 80 | list = [122,11,3434,21,656,8,3223,2] 81 | max =list[0] 82 | min=list[0] 83 | for i in list: 84 | if i>max: 85 | max = i 86 | elif i 99): 130 | print("Invalid Input!") 131 | else: 132 | f,l = a // 10,a % 10 133 | s = f+l 134 | c = [f,s,l] 135 | f = ''.join(str(c) for c in c) 136 | print(f) 137 | 138 | 139 | A = int(input("Enter A--")) 140 | B = int(input("Enter B--")) 141 | vedik(A,B) 142 | 143 | def mul(a,b): 144 | c =a*b 145 | print(c) 146 | 147 | A = int(input("Enter A--")) 148 | B = int(input("Enter B--")) 149 | mul(A,B) 150 | 151 | 152 | 153 | # Program to show Element count String 154 | st = "MeenaJitu" 155 | l = len(st) 156 | c = 1 157 | new = '' 158 | for i in range(l-1): 159 | if st[i]==st[i+1]: 160 | c =c+1 161 | else: 162 | new = new +st[i]+str(c) 163 | print(st) 164 | print(new) 165 | 166 | #Python program to implement matrix addition 167 | 168 | X = [[8,5,1], 169 | [9 ,3,2], 170 | [4 ,6,3]] 171 | Y = [[8,5,3], 172 | [9,5,7], 173 | [9,4,1]] 174 | result = [[0,0,0], 175 | [0,0,0], 176 | [0,0,0]] 177 | 178 | for i in range(len(X)): 179 | for j in range(len(Y)): 180 | result[i][j]=X[i][j]+Y[i][j] 181 | for k in result: 182 | print(k) 183 | 184 | 185 | 186 | #Python program to find second largest number in a list 187 | 188 | list1 = [10, 20, 4, 45, 99] 189 | 190 | list2 = list(set(list2)) 191 | 192 | list2.sort() 193 | print("Second largest element is:", list2[-2]) 194 | 195 | 196 | # Python Program to Return the Length of the Longest Word from the List of Words 197 | 198 | max = len(a[0]) 199 | temp = a[0] 200 | for i in a: 201 | if(len(i)>max1): 202 | max1=len(i) 203 | temp=i 204 | print("The word with the longest length is:") 205 | print(temp) 206 | 207 | 208 | # Check if a pair exists with given sum in given List 209 | def twoSum(self, nums: List[int], target: int) -> List[int]: 210 | l = len(nums) 211 | for i in range(0,l): 212 | for j in range(i+1,l): 213 | if(nums[i]+nums[j]==target): 214 | return [i,j] 215 | 216 | nums = [1,3,4] 217 | target = 5 218 | twoSum(nums,target) 219 | 220 | 221 | # Python Program to Flatten a List without using Recursion 222 | 223 | l = a=[[1,[[2]],[[[3]]]],[[4],5]] 224 | c = [] 225 | while l: 226 | el = l.pop(0) 227 | if type(el) == list: 228 | l = el+l 229 | else: 230 | c.append(el) 231 | print(c) 232 | 233 | 234 | 235 | 236 | 237 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Problem--Solving 2 | Python Coding Question all types List,Tuples, dict and String 3 | -------------------------------------------------------------------------------- /Strings.py: -------------------------------------------------------------------------------- 1 | # Program to reverse word of String 2 | 3 | 4 | 5 | str = "geeks quiz practice code" 6 | 7 | str_list = str.split() 8 | 9 | print(str_list) 10 | 11 | for str in range(len(str_list)): 12 | print(str) 13 | 14 | 15 | # Program to count Upper case and lower Case 16 | 17 | string= 'PythonMeena' 18 | u_c = 0 19 | l_c =0 20 | 21 | for i in string: 22 | if i.isupper(): 23 | u_c = u_c+1 24 | else: 25 | l_c = l_c+1 26 | 27 | print(u_c,l_c) 28 | 29 | 30 | # Reverse String in Python...... 31 | 32 | 33 | string = "Python Developer" 34 | 35 | revv = string[::-1] 36 | 37 | rev = ''.join(reversed(string)) 38 | 39 | print(rev,'/n',revv) 40 | 41 | str ="" 42 | 43 | for i in string: 44 | str = i+str 45 | print(str) 46 | 47 | 48 | #Python Program to Remove Odd Indexed Characters in a string 49 | string=input("Enter string:") 50 | new = "" 51 | for s in range(len(string)): 52 | if i%2==0: 53 | new = new+string[i] 54 | print(new) 55 | 56 | #Python Program to Remove the nth Index Character from a Non-Empty String 57 | 58 | string = input("Enter STRT-") 59 | index = int(input("Enter Index-")) 60 | print(string[:index]+string[index+1:]) 61 | 62 | 63 | # Python Program to Calculate the Length of a String Without using Library Functions 64 | 65 | 66 | string = "Jitendra" 67 | c = 0 68 | for s in string: 69 | c = c+1 70 | print(c) 71 | 72 | # Python Program to Count the Number of Words and Characters in a String 73 | 74 | string = "Jitendra Meena Python Developer" 75 | w = 1 76 | c = 0 77 | 78 | for s in string: 79 | c = c+1 80 | if(s==' '): 81 | w = w+1 82 | print(w,c) 83 | 84 | # Python Program to Count Number of Lowercase Characters in a String 85 | 86 | s = "Jitendra" 87 | 88 | l = 0 89 | for s in s: 90 | if(s == s.lower()): # islower() 91 | l=l+1 92 | print(l) 93 | 94 | # Remove Odd Indexed Characters in a string 95 | 96 | string = "Jitendra Meena" 97 | final_str = "" 98 | for st in range(len(string)): 99 | if st%2==0: 100 | final_str += string[st] 101 | print(final_str) 102 | 103 | # Remove the nth Index Character from a Non-Empty String 104 | 105 | 106 | string = "Jitendra Meena" 107 | n = int(input("enter numbers:-")) 108 | print(string[:n]+string[n+1:]) 109 | 110 | # Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively 111 | 112 | # With Count fucntion 113 | ele = input("Enter a Search str:-") 114 | string = input("Enter a String:-") 115 | print(string.count(ele)) 116 | 117 | 118 | # Without Count 119 | c = 0 120 | for i in range(len(string)): 121 | if string[i]==ele: 122 | c+=1 123 | print(c) 124 | 125 | # Program To show count of repeted elements 126 | 127 | s = "aaabbccz" 128 | char = "" 129 | ch = s[0] 130 | c = 0 131 | for st in s: 132 | if st == ch: 133 | c += 1 134 | else: 135 | char = char+str(c)+ch 136 | ch = st 137 | c = 1 138 | char = char+str(c)+ch 139 | print(char) 140 | # Output : 3a2b2c1z 141 | 142 | # How do you find the first non-repeated character in a string? 143 | s = "abcdeff" 144 | d = {} 145 | for char in s: 146 | d[char] = d.get(char ,0)+1 147 | print(d) 148 | s2 ="" 149 | for ch in s: 150 | if d[ch] == 1: 151 | s2 += ch 152 | print(s2) 153 | 154 | # How would you convert a string to uppercase or lowercase Without using Built-in ? 155 | 156 | name = "abcdeff" 157 | u_c = "" 158 | for char in name: 159 | if 'a' <= char <= 'z': 160 | u_c += chr(ord(char)-32) 161 | print(u_c) 162 | 163 | -------------------------------------------------------------------------------- /Tuples.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jitendra-meena/Python-Problem-Solving/ea9430711e7b272ad83db0a6434bcece4b1a6946/Tuples.py -------------------------------------------------------------------------------- /basic-programs.py: -------------------------------------------------------------------------------- 1 | 2 | """ 3 | Python Basic Programs 2023 by Code with Jeet 4 | 5 | """ 6 | 7 | # Python Program to Reverse a Numbers 8 | 9 | 10 | n = int(input("Enter a Numbers :-")) 11 | r = 0 12 | while(n>0): 13 | d = n%10 14 | r = r*10+d 15 | n = n//10 16 | print(r) 17 | 18 | # Python Program to Find Sum of Digits of a Number 19 | 20 | n = int(input("Enter a Numbers :-")) 21 | r = 0 22 | total = 0 23 | while(n>0): 24 | d = n%10 25 | r = r*10+d 26 | n = n//10 27 | total +=d 28 | print(total) 29 | 30 | 31 | # Python Program to Count the Number of Digits in a Number 32 | 33 | 34 | n=int(input("Enter number:")) 35 | count=0 36 | while(n>0): 37 | count=count+1 38 | n=n//10 39 | print("The number of digits in the number are:",count) 40 | 41 | # Generator in Python 42 | ''' 43 | A generator function is a special type of function in Python that returns a generator object. 44 | A generator is an iterable object that generates values one at a time, 45 | instead of generating a list with all values at once. 46 | 47 | ''' 48 | 49 | def fibonacci_generator(): 50 | a, b = 0, 1 51 | while True: 52 | yield a 53 | a, b = b, a + b 54 | 55 | fib = fibonacci_generator() 56 | for i in range(10): 57 | print(next(fib)) 58 | ''' 59 | Output: 60 | Copy code 61 | 0 62 | 1 63 | 1 64 | 2 65 | 3 66 | 5 67 | 8 68 | 13 69 | 21 70 | 34 71 | ''' 72 | ''' 73 | In this example, the fibonacci_generator function returns a generator that generates the Fibonacci sequence. 74 | The generator is assigned to the variable fib, and we can iterate over it using the for loop or the next function. 75 | The function is executed each time we call next(fib), and it stops when it encounters the end of the function. 76 | ''' 77 | 78 | ''' 79 | 80 | map is used to apply a function to each element of an iterable 81 | (e.g., a list) and return a new iterable with the results. 82 | 83 | ''' 84 | 85 | 86 | # Genrate Random Numbers Without using Built-in 87 | 88 | 89 | import time 90 | 91 | def random_number(seed=None): 92 | a = 1664525 93 | c = 1013904223 94 | m = 2**32 95 | seed = seed if seed is not None else int(time.time()) 96 | seed = (a * seed + c) % m 97 | 98 | return (seed % 100) + 1 99 | 100 | # Generate a single random number 101 | random_value = random_number() 102 | 103 | # Print the random value 104 | print("Random number:", random_value) 105 | 106 | 107 | # Fibonacci sequence using a generator in Python. 108 | def gen(n): 109 | a, b = 0, 1 110 | while a < n: 111 | yield a 112 | a, b = a + b, b 113 | 114 | # Create a generator object 115 | c = gen(10) 116 | 117 | # Call __next__() multiple times 118 | print(c.__next__()) # 0 119 | print(c.__next__()) # 1 120 | print(c.__next__()) # 1 121 | print(c.__next__()) # 2 122 | print(c.__next__()) # 3 123 | print(c.__next__()) # 5 124 | print(c.__next__()) # 8 125 | print(c.__next__()) # 13 126 | --------------------------------------------------------------------------------