Skip to content

Commit 3b36dfa

Browse files
added problems from forked to main repo (#132)
* Problem 9 Palindrome Number in ruby * [LeetCode-26] Remove duplicates from an array. * count remaining brackets * added missing code * 2 more ruby questions * begin with sorting algos * bubble sort recursive * bubble sort itrative * corrected bubble sort error * selection sort * code to convert arr of hashes into single hash * sort hash by key size * rubocop fixed * find missing number in game * do multiply without sum * operator * mremoved nested loop * insertion sort incomplete * insertion sort * find largest sum of subarray from number array * puting zeros at the end of the array * fixed the code of shifting zeros at end of array * make template for problem to solve later * removing specials characters from array problem still in progress * removes special character from array algo is complete * created template to start on problem * started problem to write algo in n time complexcity max frquency of char * tried again but faild to make logic * finally done max frequncy in string in n time complexcity * template for query * js phone shop template * query solved * js phone shop code * template ready * added questions --------- Co-authored-by: AreebAlam99 <[email protected]>
1 parent ff1bf9b commit 3b36dfa

20 files changed

+345
-28
lines changed

Diff for: C++/put_zeros_at_end_of_array.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Name: Shoaib Sabir
2+
// Username: Shoaib019
3+
// Question: given a array of numbers move all zeros to the end of the array
4+
// Approach: take two pointers one at end and one at beginning of array
5+
// use while loop to terminate when both pointers colid
6+
// inside loop just swap when non zero at beginning pointer
7+
// and when non zero at end pointer otherwise just increase beginning pointer
8+
class Solution {
9+
public:
10+
void append_zeros(int nums [],int size) {
11+
int last_indx = size - 1;
12+
int start_indx = 0;
13+
while(start_indx < last_indx)
14+
{
15+
if(nums[start_indx] == 0)
16+
{
17+
if(nums[last_indx] == 0)
18+
{
19+
last_indx--;
20+
}
21+
else
22+
{
23+
nums[start_indx] = nums[last_indx];
24+
nums[last_indx] = 0;
25+
start_indx++;
26+
last_indx--;
27+
}
28+
29+
}
30+
else
31+
{
32+
start_indx++;
33+
}
34+
}
35+
}
36+
};

Diff for: C++/remove_special_characters_from_aaray.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Name: Shoaib Sabir
2+
// Username: Shoaib019
3+
// Question: remove all special characters from characters array
4+
// Approach: remove_character shifts array to let by one character
5+
// is_special_character checks whether character is special or not
6+
// remove_special_charactes reduce size if last character is special
7+
// and runs loop whenever special character finds in the array it removes
8+
// find special character using is_special_character method and remove
9+
// special charactes using remove_character method
10+
class Solution {
11+
public:
12+
int remove_character(char arr [], char ch, int size){
13+
int i;
14+
for(i=0; i< size; i++)
15+
{
16+
if(arr[i] == ch)
17+
break;
18+
}
19+
if(i < size)
20+
{
21+
size--;
22+
for(int j=i; j < size; j++)
23+
{
24+
arr[j] = arr[j + 1];
25+
}
26+
arr[size + 1] = 0;
27+
}
28+
return size;
29+
}
30+
31+
bool is_special_character(char ch){
32+
if((!(ch >= 97 && ch <= 122) && !(ch >= 65 && ch <= 90) ))
33+
return true;
34+
else
35+
return false;
36+
}
37+
38+
int remove_special_charactes(char arr [],int size) {
39+
if(is_special_character(arr[size - 1]))
40+
{
41+
size--;
42+
}
43+
for(int i=0; i< size; i++)
44+
{
45+
if(is_special_character(arr[i]))
46+
{
47+
size = remove_character(arr,arr[i],size);
48+
i--;
49+
}
50+
}
51+
return size;
52+
}
53+
};

Diff for: JS/phone_shop.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Name: Shoaib Sabir
2+
// Username: Shoaib019
3+
// Question: find out you can afford phone or not by calculating
4+
// phone price with accessories also add tax
5+
6+
// DATA
7+
TAX_RATE = 0.08;
8+
PHONE_PRICE = 99.99;
9+
ACCESSORY_PRICE = 9.99;
10+
SPENDING_THRESHOLD = 200;
11+
var bank_account_balance = 303.91;
12+
13+
console.log(calculatePurchaseAmount()); // this is starting point
14+
15+
function calculatePurchaseAmount() {
16+
totalSpent = calculateTotalPhonePrice();
17+
totalAmount = totalSpent + calculatingTax(totalSpent);
18+
return formatThePrice(totalAmount);
19+
}
20+
21+
function canBuy(spent){ // this function keeps some amount in bank account
22+
remaining_bal = bank_account_balance - spent;
23+
return remaining_bal > ACCESSORY_PRICE ? true : false;
24+
}
25+
26+
function calculateTotalPhonePrice(){
27+
let spent = 0;
28+
while (canBuy(spent)){
29+
spent += PHONE_PRICE;
30+
31+
if(spent < SPENDING_THRESHOLD){
32+
spent += ACCESSORY_PRICE;
33+
}
34+
}
35+
return spent;
36+
}
37+
38+
39+
function calculatingTax(totalSpent){
40+
return totalSpent * TAX_RATE;
41+
}
42+
43+
function formatThePrice(totalAmount){
44+
return "Total Price: $" + String(totalAmount.toFixed(2));
45+
}

Diff for: MYSQL/user_name_and_count_of_invoices.sql

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-- Write an SQL query to find username and count of invoices from user and invoice table.
2+
3+
-- Return the result table in any order.
4+
-- Write your MySQL query statement below
5+
-- create a table
6+
CREATE TABLE user (
7+
id INTEGER PRIMARY KEY,
8+
name TEXT NOT NULL,
9+
gender TEXT NOT NULL
10+
);
11+
12+
CREATE TABLE invoice (
13+
id INTEGER PRIMARY KEY,
14+
name TEXT NOT NULL,
15+
total INTEGER NOT NULL,
16+
user_id INTEGER,
17+
FOREIGN KEY (user_id) REFERENCES user(id)
18+
);
19+
-- insert some values
20+
INSERT INTO user VALUES (1, 'Ryan', 'M');
21+
INSERT INTO user VALUES (2, 'Joanna', 'F');
22+
INSERT INTO user VALUES (3, 'Saul', 'M');
23+
INSERT INTO user VALUES (4, 'Norma', 'F');
24+
25+
INSERT INTO invoice VALUES (1, 'backery', 99,1);
26+
INSERT INTO invoice VALUES (2, 'meat', 88,1);
27+
INSERT INTO invoice VALUES (3, 'candy', 77,3);
28+
INSERT INTO invoice VALUES (4, 'soaps', 66,4);
29+
INSERT INTO invoice VALUES (5, 'meat', 55,2);
30+
INSERT INTO invoice VALUES (6, 'candy', 44,2);
31+
INSERT INTO invoice VALUES (7, 'soaps', 33,2);
32+
-- fetch some values
33+
SELECT user.name , COUNT(*) as count
34+
FROM user
35+
INNER JOIN invoice ON user.id = invoice.user_id
36+
GROUP BY user.id
37+
HAVING COUNT(*) > 1;

Diff for: Ruby/count_consective_numbers.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
# frozen_string_literal: true
2+
13
# Name: Shoaib Sabir
24
# Username: Shoaib019
35
# Approach: make hash count as value and number as key then find max value return its key
46
def count_consective_numbers(arr)
57
hash_one = {}
68
arr.each do |val|
7-
hash_one[val] = arr.count(val)
9+
hash_one[val] = arr.count(val)
810
end
9-
hash_one.max_by {|k,v| v}.first
11+
hash_one.max_by { |_k, v| v }.first
1012
end

Diff for: Ruby/count_max_letter_in_array.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
# Name: Shoaib Sabir
4+
# Username: Shoaib019
5+
# Approach: take hash and initialize it with zero run loop over string
6+
# increase count every time in hash key which is character
7+
# then use max_by built in method to find letter with max count
8+
def find_max_frequency_character(str)
9+
hash = Hash.new(0)
10+
str.each_char do |ch|
11+
hash[ch] += 1
12+
end
13+
hash.max_by { |_, v| v }.first
14+
end

Diff for: Ruby/count_remaining_brackets.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
# frozen_string_literal: true
2+
13
# Name: Shoaib Sabir
24
# Username: Shoaib019
35
# Approach: deduct open from close bracket to get remaining brackets
46
def count_remaining_brackets(str)
5-
open_brackets = str.count('(')
6-
close_brackets = str.count(')')
7-
remaining = open_brackets - close_brackets
8-
remaining.negative? ? remaining.abs : remaining
9-
end
7+
open_brackets = str.count('(')
8+
close_brackets = str.count(')')
9+
remaining = open_brackets - close_brackets
10+
remaining.negative? ? remaining.abs : remaining
11+
end

Diff for: Ruby/fizzbuzz.rb

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
# frozen_string_literal: true
2+
13
# Name: Shoaib Sabir
24
# Username: Shoaib019
3-
# Approach: loop runs n times for each element and checks for appropriate conditions
4-
def fizz_buzz(n)
5-
arr = []
6-
(1..n).each do |num|
7-
if (num.modulo(3).zero? && num.modulo(5).zero?) then arr << "FizzBuzz"
8-
elsif num.modulo(3).zero? then arr << "Fizz"
9-
elsif num.modulo(5).zero? then arr << "Buzz"
10-
else arr << num.to_s
11-
end
12-
end
13-
arr
14-
end
5+
# Approach: loop runs number times for each element and checks for appropriate conditions
6+
def fizz_buzz(number)
7+
arr = []
8+
(1..number).each do |num|
9+
arr << if num.modulo(3).zero? && num.modulo(5).zero? then 'FizzBuzz'
10+
elsif num.modulo(3).zero? then 'Fizz'
11+
elsif num.modulo(5).zero? then 'Buzz'
12+
else
13+
num.to_s
14+
end
15+
end
16+
arr
17+
end

Diff for: Ruby/inventing_multiplication.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
# Name: Shoaib Sabir
4+
# Username: Shoaib019
5+
# Question: do multiplication without * operator
6+
def inventing_multiplication(num1, num2)
7+
res = 0
8+
num2.times.each do |_num|
9+
res += num1
10+
end
11+
res
12+
end

Diff for: Ruby/jump_tiles.rb

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
# frozen_string_literal: true
2+
13
# Name: Shoaib Sabir
24
# Username: Shoaib019
5+
# Question: jump n times where n is current number in array and find did you able to reach to end of array
36
# Approach: in loop added only those elements on which pointer value is same
47
# then compare pointer value with size of array
58
def jump_tiles(arr)
6-
pointer = 0
7-
arr.each_with_index do |_,idx|
8-
pointer = pointer + arr[idx] if pointer == idx
9-
end
10-
pointer >= arr.length - 1 ? true : false
11-
end
9+
pointer = 0
10+
arr.each_with_index do |_, idx|
11+
pointer += arr[idx] if pointer == idx
12+
end
13+
pointer >= arr.length - 1
14+
end

Diff for: Ruby/largest_sum.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
def largest_sum; end

Diff for: Ruby/largest_sum_of_numbers_subarray.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
# Name: Shoaib Sabir
4+
# Username: Shoaib019
5+
# Approach:
6+
def largest_sum(arr)
7+
# code here
8+
end

Diff for: Ruby/make_one_hash.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
def make_one_hash(arr)
4+
hash = {}
5+
arr.each do |ele|
6+
hash.merge!(ele)
7+
end
8+
hash
9+
end
10+
11+
arr = [{ a: 3, b: 2, c: 3, d: 4 }, { e: 3, f: 2 }, { g: 2, h: 3 }]
12+
puts make_one_hash(arr)

Diff for: Ruby/missing_game_number.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
# Name: Shoaib Sabir
4+
# Username: Shoaib019
5+
# Question: find missing number in array
6+
def missing_game_number(arr)
7+
(1..10).each do |num|
8+
arr << num if arr.exclude?(num)
9+
end
10+
arr.sort
11+
end

Diff for: Ruby/palindrome_number.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
# frozen_string_literal: true
2+
13
# Name: Areeb Alam
24
# Username: AreebAlam99
35
# Approach: Converted the number into an array of digits and checked if it was equal to its reverse or not.
46

5-
def is_palindrome(x)
6-
return false if x < 0
7-
x.digits == x.digits.reverse ? true : false
7+
def palindrome?(num)
8+
return false if num.negative?
9+
10+
num.digits == num.digits.reverse
811
end

Diff for: Ruby/remove_duplicates_from_array.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# Name: Areeb Alam
24
# Username: AreebAlam99
35
# Approach: Just used uniq to remove duplicates and returned count :p.

Diff for: Ruby/sort_hash_by_key_size.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
hash = { "abcd": 4, "abc": 3, "abcdf": 5, "ab": 2 }
4+
puts Hash[hash.sort_by { |k, _| k.size }]

Diff for: sorting_algos/bubble_sort.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// recursive approach
2+
3+
void bubble_sort(int arr[],int size)
4+
{
5+
if (size == 0 || size == 1)
6+
return;
7+
for(int i = 0; i < size - 1 ; i++)
8+
{
9+
if(arr[i] > arr[i + 1])
10+
{
11+
int temp = arr[i];
12+
arr[i] = arr[i + 1];
13+
arr[i + 1] = temp;
14+
}
15+
}
16+
bubble_sort(arr, size - 1);
17+
}
18+
19+
// itrative approach
20+
void bubble_sort(int arr[],int size)
21+
{
22+
for(int i = 0; i < size - 1 ; i++)
23+
{
24+
for(int j = 0; j < size - i - 1; j++)
25+
{
26+
if(arr[j] > arr[j + 1])
27+
{
28+
int temp = arr[j];
29+
arr[j] = arr[j + 1];
30+
arr[j + 1] = temp;
31+
}
32+
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)