How to Count the Number of Words in a String in Python

Problem Formulation Given a string – sentence. How many words does the string sentence have within it? Examples: INPUT sentence = “Finxter helps you to master Python.” OUTPUT Number of words: 6 INPUT sentence = “””Lorem ipsum dolor sit amet. Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.””” OUTPUT … Read more

How to Count the Number of Unique Values in a List in Python?

Problem Statement:ย Consider that you have been given a list in Python. How will you count the number of unique values in the list? Example: Let’s visualize the problem with the help of an example: Given:ย li = [‘a’, ‘a’, ‘b’, ‘c’, ‘b’, ‘d’, ‘d’, ‘a’]Output:ย The unique values in the given list are ‘a’, ‘b’, ‘c’, ‘d’. … Read more

Python – How to Find the Longest Substring in Alphabetical Order?

Programming Challenge ๐Ÿ’ฌ Challenge: Given a Python string. Find the longest substring for which holds that all of its characters are in alphabetical order so that the first character comes earlier in the alphabet than the second character, and so on. Here are three examples: You get the point. ๐Ÿ™‚ Next, I’ll show you three … Read more

How to Find Number of Digits in an Integer?

To find the number of digits in an integer you can use one of the following methods: (1) Use Iteration (2) Use str()+len() functions (3) Use int(math.log10(x)) +1 (4) Use Recursion Problem Formulation Given: An integer value. Question: Find the number of digits in the integer/number given. Test Cases: Input: num = 123 Output: 3 … Read more

Python Palindromes One-Liner

This one-liner introduces another basic computer science term: palindromes. Similar to anagrams, palindromes are a popular coding interview question. First things first: What is a Palindrome? โ€œA palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar or the number 10201.โ€œ [source] … Read more

[Google Interview] The 3 Sum Problem

Company Tags: Google, Adobe, Amazon, Apple, Bloomberg, Facebook, Oracle, Microsoft, Tesla Problem Statement Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Note: that the solution set must not contain duplicate triplets. … Read more

[Google Interview] Detect Capital

[toc] Company Tag: Google Problem Formulation We define the usage of capitals in a word to be right when one of the following cases holds: Rule 1: All letters in this word are capitals, like “USA”. Rule 2: All letters in this word are not capitals, like “welcome”. Rule 3: Only the first letter in … Read more

[Google Interview] Shuffle the Array

[toc] Company Tags: Google, Adobe, Apple, Bloomberg, Microsoft Problem Description Given the array nums consisting of 2n elements in the form [x1, x2,…,xn, y1, y2,…, yn]. Return the array in the form [x1, y1, x2, y2,…, xn, yn]. Constraints: 1 <= n <= 500 nums.length == 2n 1 <= nums[i] <= 10^3 Examples Let’s have … Read more