1394. Find Lucky Integer in an Array [Leetcode]

Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value.

Return a lucky integer in the array. If there are multiple lucky integers return the largest of them. If there is no lucky integer return -1.

Example 1:

Input: arr = [2,2,3,4]
Output: 2
Explanation: The only lucky number in the array is 2 because frequency[2] == 2.

Example 2:

Input: arr = [1,2,2,3,3,3]
Output: 3
Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.

Example 3:

Input: arr = [2,2,2,3,3]
Output: -1
Explanation: There are no lucky numbers in the array.

Example 4:

Input: arr = [5]
Output: -1

Example 5:

Input: arr = [7,7,7,7,7,7,7]
Output: 7

Constraints:

  • 1 <= arr.length <= 500
  • 1 <= arr[i] <= 500

Solution:

/**
 * https://leetcode.com/problems/find-lucky-integer-in-an-array
 * github: theketan2
 * @param {number[]} arr
 * @return {number}
 */
var findLucky = function (arr) {
  arr.sort((a, b) => b - a);
  for (let i = 0; i < arr.length; i++) {
    let len = arr.filter((num) => num === arr[i]).length;
    console.log(len, arr[i]);
    if (len === arr[i]) {
      return len;
    }
  }
  return -1;
};

Number of Apples Left [Edabit]

A man has n number of apples. If he eats a percentage p of the apples (if apples are available), his children will share the remainder of the apples. Create a function to determine the number of ‘whole’ apples his children got. If his children did not get any apples, return "The children didn't get any apples".

Examples

get_number_of_apples(10, "90%") ➞ 1

get_number_of_apples(25, "10%") ➞ 22

get_number_of_apples(0, "10%") ➞ "The children didn't get any apples"

Solution:

import math
def get_number_of_apples(n, p):
	if n == 0 or int(p[:-1]) == 100:
		return "The children didn't get any apples"
	return math.floor(n- n* int(p[:-1])/100)
	

412. Fizz Buzz [Leetcode]

Leetcode

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three, it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

Solution:

/**
 * url: https://leetcode.com/problems/fizz-buzz
 * github: theketan2
 * @param {number} n
 * @return {string[]}
 */
var fizzBuzz = function (n) {
  let sol = [];
  for (let i = 1; i <= n; i++) {
    if (i % 3 == 0 && i % 5 == 0) {
      sol.push("FizzBuzz");
    } else if (i % 3 == 0) {
      sol.push("Fizz");
    } else if (i % 5 == 0) {
      sol.push("Buzz");
    } else {
      sol.push(i.toString());
    }
  }
  return sol;
};

Correct My Sentence [Edabit]

Edabit

Mubashir is not so good with the English language. He needs your help to correct his sentences.

  1. Start each sentence with an uppercase alphabet.
  2. For every uppercase letter (other than the first alphabet), you have to place a fullstop(.) followed by an empty space.
  3. There must be only one space between the words and sentences.
  4. Sentence must end with a full stop(.)
  5. Two continuous spaces are not allowed.
correct_sentences ("  mubashir loves  edabit  Matt  loves  edabit  ") ➞ "Mubashir loves edabit. Matt loves edabit."

# Remove extra spaces.
# Capitalise first character.
# Dot followed by an empty space before "Matt".
# A dot at the end.

Examples

correct_sentences ("  mubashir loves  edabit  Matt  loves  edabit  ") ➞ "Mubashir loves edabit. Matt loves edabit."

correct_sentences ("  he is an engineer He sleeps a lot") ➞ "He is an engineer. He sleeps a lot."

correct_sentences (" his english is not good Help him     Thank you") ➞ "His english is not good. Help him. Thank you."

Solution:

def correct_sentences(s):
	sentense = s.strip().split()
	sol = ""
	for i in range(len(sentense)):
		if i == 0:
			sol += sentense[i].title()
		elif sentense[i][0] == sentense[i][0].upper():
			sol += ". "+sentense[i]
		else:
			sol += " "+sentense[i]
	return sol+"."

509. Fibonacci Number [Leetcode]

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.

Given n, calculate F(n).

Example 1:

Input: n = 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: n = 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

Constraints:

  • 0 <= n <= 30

Solution:

/**
 * https://leetcode.com/problems/fibonacci-number/
 * github: theketan2
 * @param {number} n
 * @return {number}
 */
var fib = function (n) {
  // 0 1 1 2 3 5 8 13 21
  sol = [0, 1, 1, 2, 3, 5];
  if (n < sol.length) {
    return sol[n];
  }
  console.log("hello");
  while (sol.length != n + 1) {
    let len = sol.length;
    console.log(sol);
    sol.push(sol[len - 1] + sol[len - 2]);
  }
  console.log(sol);
  return sol[n];
};

Car Timer 🏎️[Edabit]

A built-in timer inside your car can count the length of your ride in minutes and you have started your ride at 00:00.

Given the number of minutes n at the end of the ride, calculate the current time. Return the sum of digits that the digital timer in the format hh:mm will show at the end of the ride.

Examples

car_timer(240) ➞ 4
# 240 minutes have passed since 00:00, the current time is 04:00
# Digits sum up is 0 + 4 + 0 + 0 = 4

car_timer(808) ➞ 14

car_timer(14) ➞ 5

Solution:

def car_timer(n):
	time = list(str(n//60) + str(n%60))
	sol = sum([int(i) for i in time])
	return sol
	
	

Scottish Screaming [Edabit]

Edabit

A strong Scottish accent makes every vowel similar to an “e”, so you should replace every vowel with an “e”. Additionally, it is being screamed, so it should be in block capitals.

Create a function that takes a string and returns a string.

Examples

to_scottish_screaming("hello world") ➞ "HELLE WERLD"

to_scottish_screaming("Mr. Fox was very naughty") ➞ "MR. FEX WES VERY NEEGHTY"

to_scottish_screaming("Butterflies are beautiful!") ➞ "BETTERFLEES ERE BEEETEFEL!"

Solution:

def to_scottish_screaming(txt):
	lst = list(txt.upper())
	for i in range(len(lst)):
		if "AEIOU".count(lst[i]) != 0:
			lst[i] = "E"
	return "".join(lst)
	
	

Pronouncing the Xs [Edabit]

Edabit

Create a function which replaces all the x’s in the string in the following ways:

Replace all x’s with “cks” UNLESS:

  • The word begins with “x”, therefore replace it with “z”.
  • The word is just the letter “x”, therefore replace it with “ecks”.

Examples

x_pronounce("Inside the box was a xylophone") ➞ "Inside the bocks was a zylophone"

x_pronounce("The x ray is excellent") ➞ "The ecks ray is eckscellent"

x_pronounce("OMG x box unboxing video x D") ➞ "OMG ecks bocks unbocksing video ecks D"

Solution:

def x_pronounce(sentence):
	strList = sentence.split(" ")
	sol = ""
	for word in strList:
		if len(word) == 1 and word == "x":
			sol += " ecks"
		elif word[0] == "x":
			sol += (" z"+ word[1:].replace("x", "cks"))
		else:
			sol += " "+word.replace("x", "cks")
	return sol.strip()
	

Cricket Balls to Overs! [Edabit]

Edabit

In cricket, an over consists of six deliveries a bowler bowls from one end. Create a function that takes the number of balls balls bowled by a bowler and calculates the number of overs and balls bowled by him/her. Return the value as a float, in the format overs.balls.

Examples

total_overs(2400) ➞ 400

total_overs(164) ➞ 27.2
# 27 overs and 2 balls were bowled by the bowler.

total_overs(945) ➞ 157.3
# 157 overs and 3 balls were bowled by the bowler.

total_overs(5) ➞ 0.5

Solution:

import math
def total_overs(balls):
	if balls%6 == 0: 
		return balls/6
	return balls%6/10 + balls//6

961. N-Repeated Element in Size 2N Array [Leetcode]

Leetcode

In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times.

Return the element repeated N times.

Example 1:

Input: [1,2,3,3]
Output: 3

Example 2:

Input: [2,1,2,5,3,2]
Output: 2

Example 3:

Input: [5,1,5,2,5,3,5,4]
Output: 5

Note:

  • 4 <= A.length <= 10000
  • 0 <= A[i] < 10000
  • A.length is even

/**
 * @param {number[]} A
 * @return {number}
 */
var repeatedNTimes = function(A) {
    let lenN = A.length/2
    for(let num of [...A]){
        const n = A.filter(elem => elem === num)
        if(n.length == lenN)
            return num
    }
};