Unsorted Examples
Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list.
A node in a singly linked list should have two attributes: val
and
next. val is the value of the current node
next is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list.
Assume all nodes in the linked list are 0-indexed.
Implement these functions in your linked list class:
Define Count Vowels Function
Count the number of vowels in the user input string using for loop and while loop
Now, let’s define a function named countVowels() which accepts one argument as a string where we need to count Vowels in it.
So, declare a variable count and initialize to 0 which keeps the track of vowel count.
Now before counting the vowels, first make sure the string is completely lowercase because we are checking vowels from the list where we declared vowel in lowercase only.
So keep it in mind to the first convert string to lowercase and for that, we are going to use lower() function of Python.
So, we have converted the string to lowercase, now traverse through the string and compare each character of string in the list of vowels whether the character is present in the list or not.
If the character is present in the list, then we simply increment the count variable by 1 otherwise continue to the loop without incrementing the count variable.
Read => Check if a number is greater than all those numbers in the list
And at last, after traversing through the string, return the count variable back to the function call.
Define the Main Condition
Now, we have defined the Count Vowel function above which calculates the count of the number of vowels in the string.
So after this, ask the user to enter string which needs to pass to this above function and then print the count for the vowel.
Code
Output
Define Binary Number to Decimal Function
Let’s create a new function to Convert Binary to a Decimal Number named binaryToDecimal() which accepts Binary as an argument.
So let’s store the argument in the separate variables to use that at last while printing what the actual data is.
Along with this, create two new variables “decimal” and “i” and initialized both to 0.
Now create a while loop that loops till the number which we received as an argument not become 0.
So the logic behind converting is that first in each iteration, we need to get the last digit of Number either 1 or 0 and then multiply that last digit with the power of 2 and counter i.
Read => Python Program to Calculate LCM of Two Numbers
And at last, we divide the number by 10 and assign the same number back to binary to reduce it to 0.
Now, simply print the result using the format function in Python.
Define the Main Method
Let’s create the main method which is going to ask for user input for the Binary Number and then pass that number to function that we created above.
Code
Output
Fibonacci Series With Recursion
Let’s create a new Function named fibonacci_with_recursion() which is going to find the Fibonacci Series till the n-th term by calling it recursively.
So the base condition will be if the number is less than or equal to 1, then simply return the number.
Otherwise, return the callback to Fibonacci function again with decrement value from numbers 1 and 2 and add both function calls.
Fibonacci Series Without Recursion
Let’s create a new Function named fibonacci_without_recursion() which is going to find the Fibonacci Series till the n-th term by using FOR Loops.
Read => Program to check whether the Number is Prime or Not
So, the base condition for this function is if the number is equal to 0, then we return output as 0 because of how we calculate the Series if the number is 0.
Now, let’s declare two variables named fibonacci0 for number 0 and initialize it to 0 and fibonacci1 for number 1 and initialize it to 1.
Now create a FOR Loop to calculate till the n-th term, so the logic is simple as that assigns the sum of fibonacci0 and fibonacci1 to fibonacci1 and assigns fibonacci0 the value of fibonacci1 at last step.
And after calculating n-th term, simply returns the fibonacci1 because which keeps tracks till the n-th term.
Define the Main Method
Now let’s create a main method where we need to call these both methods that we have created above for calculating Fibonacci Series using Recursion as well as by For Loops.
First, ask for the user input to enter any number. If the number is less than 0, then simply returns an error message printing that the “Number must be Positive Number“.
Source Code
Output
Last updated