Programming Articles - Page 1739 of 3366

Base 3 to integer in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:06:55

1K+ Views

Suppose we have a string s that is representing a number in base 3 (valid numbers 0, 1, or 2), we have to find its equivalent decimal integer.So, if the input is like "10122", then the output will be 98.To solve this, we will follow these steps −ans := 0for each digit c in s, doans := 3 * ans + creturn ansLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       ans = 0       for c in map(int, s):          ans = 3 * ans + c       return ans ob = Solution() print(ob.solve("10122"))Input"10122"Output98

Austin Powers in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:05:18

122 Views

Suppose we have a number greater than 0, we have to check whether the number is power of two or not.So, if the input is like 1024, then the output will be True.To solve this, we will follow these steps −while n > 1, don := n / 2return true when n is same as 1, otherwise 0Let us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, n):       while n > 1:          n /= 2       return n == 1 ob = Solution() print(ob.solve(1024))Input1024OutputTrue

A unique string in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:02:51

970 Views

Suppose we have a string s, we have to check whether it has all unique characters or not.So, if the input is like "world", then the output will be TrueTo solve this, we will follow these steps −set_var := a new set from all characters of sreturn true when size of set_var is same as size of s, otherwise falseLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       set_var = set(s)       return len(set_var) == len(s) ob = Solution() print(ob.solve('hello')) print(ob.solve('world'))Inputhello worldOutputFalse TrueRead More

Atbash cipher in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:00:21

2K+ Views

Suppose we have a lowercase alphabet string called text. We have to find a new string where every character in text is mapped to its reverse in the alphabet. As an example, a becomes z, b becomes y and so on.So, if the input is like "abcdefg", then the output will be "zyxwvut"To solve this, we will follow these steps −N := ASCII of ('z') + ASCII of ('a')return ans by joining each character from ASCII value (N - ASCII of s) for each character s in textLet us see the following implementation to get better understanding −Example Live Democlass Solution: ... Read More

A strictly increasing linked list in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:58:56

282 Views

Suppose we have head of a singly linked list, we have to check whether the values of the nodes are sorted in a strictly ascending order or not.So, if the input is like [2, 61, 105, 157], then the output will be True.To solve this, we will follow these steps −Define a function solve() . This will take headif head.next is null, thenreturn Trueif head.val >= head.next.val, thenreturn Falsereturn solve(head.next)Let us see the following implementation to get better understanding −Example Live Democlass ListNode:    def __init__(self, data, next = None):       self.val = data       self.next = ... Read More

A number and its triple in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:56:08

716 Views

Suppose we have a list of numbers called nums, we have to check whether there are two numbers such that one is a triple of another or not.So, if the input is like nums = [2, 3, 10, 7, 9], then the output will be True, as 9 is the triple of 3To solve this, we will follow these steps −i := 0sort the list nj := 1while j < size of n, doif 3*n[i] is same as n[j], thenreturn Trueif 3*n[i] > n[j], thenj := j + 1otherwise, i := i + 1return FalseLet us see the following implementation ... Read More

Ancient Astronaut Theory in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:52:14

210 Views

Suppose er have a string dictionary, the dictionary is representing a partial lexicographic ordering of ancient astronauts' dictionary. So, if we have a string s, we have to check whether it's a lexicographically sorted string according to this ancient astronaut dictionary or not.So, if the input is like dictionary = "bdc", s = "bbbb h ddd i cccc", then the output will be TrueTo solve this, we will follow these steps −l := size of astro_dictif l is same as 0, thenreturn Truei := 0for each character c in s, doif c in astro_dict, thenwhile i < l and astro_dict[i] ... Read More

Adding Time in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:45:26

4K+ Views

Suppose we have a string that is representing a 12-hour clock time with suffix am or pm, and an integer n is also given, we will add n minutes to the time and return the new time in the same format.So, if the input is like s = "8:20pm" and n = 150, then the output will be 10:50pmTo solve this, we will follow these steps −h, m := take the hour and minute part from sh := h mod 12if the time s is in 'pm', thenh := h + 12t := h * 60 + m + nh ... Read More

Acronym in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:42:46

1K+ Views

Suppose we have a string s that is representing a phrase, we have to find its acronym. The acronyms should be capitalized and should not include the word "and".So, if the input is like "Indian Space Research Organisation", then the output will be ISROTo solve this, we will follow these steps −tokens:= each word of s as an arraystring:= blank stringfor each word in tokens, doif word is not "and", thenstring := string concatenate first letter of wordreturn convert string into uppercase stringLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):   ... Read More

Accumulator battery in Python

Akshitha Mote
Updated on 22-Jul-2025 18:50:00

326 Views

Problem Statement Consider a mobile phone, which is in "eco mode". This mode activates once your battery level reaches 20 percent. In this eco mode, the battery drains two times slower than in normal mode. This means that, in normal mode, if the time consumed by the battery is 1% per minute, then the time taken in eco mode is 1% per 30 seconds (1/2 minute). Scenario Now, when we leave our home, we have 100% of the battery. Then t minutes later we have p percent of battery left. We have to find how many minutes we have until ... Read More

Advertisements