Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished 14 algorithms #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions December 01/python3_TheVanishingNumber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# December 01 - The Vanishing Number


N = int(input("Enter the number of participants: "))

print("Enter the list in the following format, 1 2 3 4 5")
lst = list(map(int, input().split()))

# Missing number
mn = int(((N*(N+1))/2) - sum(lst))
print("The bib number of the missing participant is:", mn)


""" Solution from 1012 """
24 changes: 24 additions & 0 deletions December 02/python3_TheWaveSortChallenge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# December 02 - The Wave Sort Challenge


print("Enter the list of integers in the folllowing format, 1 2 3 4 5")
lst= list(map(int, input().split()))

# Sorting the list in ascending order
lst.sort()

# Wave Array
wa = []

while(len(lst)>1):
wa.append(lst.pop(-1))
wa.append(lst.pop(0))

# Appending the last element in the list
if lst:
wa.append(lst[0])

print(wa)


""" Solution from 1012 """
19 changes: 19 additions & 0 deletions December 03/python3_AlternatingSquareArrangement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# December 03 - Alternating Square Arrangement


R = int(input("Enter the number of red squares: "))
B = int(input("Enter the number of blue squares: "))

more = max(R, B)
less = min(R, B)

if more-1 == less or more == less:
if more == R:
print("RB"*less, "" if more==less else "R", sep="")
else:
print("BR"*less, "" if more==less else "B", sep="")
else:
print("Not Possible")


""" Solution from 1012 """
36 changes: 36 additions & 0 deletions December 04/C++_PlantGrowthTracker.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// December 04 - Plant Growth Tracker


#include<iostream>
#include<vector>
using namespace std;

int main()
{
int N;
cout<<"Enter the no of months: ";
cin>>N;

// No of the plants
int sum=0;

// Creating a vector
vector<int> vec(N, 1);

int i, value;
for(i=2; i<N ;i++)
{
vec[i] = vec[i-1] + vec[i-2];
}


cout<<vec[i-1];




return 0;
}


/* Solution from 1012 */
35 changes: 35 additions & 0 deletions December 05/python3_JosephusProblem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# December 05 - Josephus Problem


N = int(input("Enter the number of persons: "))

# No of persons to be moved
nfpm = int(input("Enter the number of persons to be counted: ")) - 1

# Creating the list of the persons
lst = list(range(1, N+1))

# The first person to be killed
index = nfpm
lst.pop(index)

while True:

# Making the list as the circle
for i in range(nfpm):
index += 1
if index == len(lst):
index= 0

lst.pop(index)

# If the index was refered the last element
index = 0 if index == len(lst) else index

if len(lst) == 1:
break

print(lst[0])


""" Solution from 1012 """
32 changes: 32 additions & 0 deletions December 06/python3_TargetPairFinder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# December 06 - Target Pair Finder


# Inputs
print("Enter the list in the following format, 1 2 3 4 5\n")

# Removing duplicated elements (set)
# as well as maintaining the order (list)
lst = list(set(map(int, input().split())))

tsum = int(input("Enter the target sum: "))

# Unique pairs
up = []

for ele1 in lst:

index = lst.index(ele1)

for ele2 in lst[index:]:
if ele1 + ele2 == tsum:
pair = (ele1, ele2)
if pair in up:
pass
else:
up.append(pair)


print(up)


""" Solution from 1012 """
29 changes: 29 additions & 0 deletions December 07/python3_TheMagicalTower.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# December 07 - The Magical Tower


# No of rows
N = int(input("Enter the number of rows: "))

# The list that will contain all the floors
lst = []

for floor in range(1, N+1):

# Temporary list
tlst = []

# Making every elements of the floor to one
for i in range(0, floor):
tlst.append(1)

# Replacing Non-one elements in the respective floors' indices
if floor >= 3:
for i in range(1, floor-1):
tlst[i] = lst[-1][i] + lst[-1][i-1]

lst.append(tlst)

print(lst[0] if N == 1 else lst)


""" Solution from 1012 """
33 changes: 33 additions & 0 deletions December 08/python3_DigitManipulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# December 08 - Digit Manipulation


# Return the digits of the num in a list
def return_digits(num):
digits = []

while (num > 0):
digit = num % 10
digits.append(digit)

num = int(num/10)

return digits

# Required function
def digit_square_sum(N):
sum = 0

for i in range(1, N+1):
if (i<=9): sum += i**2
else:
for digit in return_digits(i):
sum += digit**2
return sum


num = int(input("Enter the number: "))
print("The Total Digit square sum is ",digit_square_sum(num))


""" Solution from 1012 """

5 changes: 5 additions & 0 deletions December 09/python3_CustomerReturnFrequency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# December 09 - Customer Return Frequency

print("Enter the list in the following format, 1 2 3 4 5 \n")
returns = list(map(int, input().split()))
print(returns.count(1))
93 changes: 93 additions & 0 deletions December 10/python3_ConcurrentTaskExecution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# December 10 - Concurrent Task Execution


"""
---------------- Important to note ----------------

-> I am not really sure how to get the task list from the console in a more efficient manner

-> So the following approach is taken to get the input

--> It is reccomended to just assign the list to the tasks variable in the source code itself!!
(line-17)

---------------------------------------------------------------
"""

tasks = []
# Order of execution
oe = [[]]

np = int(input("Enter the number of pairs: "))
print("Enter the pairs one by one in the following format, A B,C which is ('A', ['B', 'C'])\n")

for i in range(np):
pair = input().split()
ID = pair[0]
dep = pair[1].split(",") if len(pair) == 2 else []
tasks.append((ID, dep))


# Sorting based on the number of dependencies
tasks = sorted(tasks, key=lambda item: len(item[1]))


# Check if all the dependencies are present!
def is_there(depn):
tot = len(depn)
n = 0

order = 0

for t in depn:
for o in oe:
if t in o:
n+=1
order = oe.index(o) if order < oe.index(o) else order

# Checking if all the dependencies are there
if n == tot:
# Returning in which order the latest dependency runs + 1
return (True, order+1)
else:
return (False, -1)


# Parsing
# To loop the tasks list
index = 0
# To ensure that we looped it only two times
count = 0

while(tasks and count < 2):

if index == len(tasks):
index = 0
count += 1

id = tasks[index][0]
dpn = tasks[index][1]

if dpn == []:
oe[index].append(id)
tasks.pop(0)
else:
value, order = is_there(dpn)
if value:
try:
oe[order].append(id)
tasks.pop(index)
except:
oe.append([])
oe[order].append(id)
tasks.pop(index)
else:
index += 1

if count == 2:
print("Error: Cyclic dependency detected")
else:
print(oe)


""" Solution from 1012 """
24 changes: 24 additions & 0 deletions December 11/python3_TheRobotReturns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# December 11 - The Robot Returns


moves = list(input("Enter the moves sequence as a string: "))

pos = [0, 0]

for move in moves:

# x-axis
if move == 'R':
pos[0]+=1
elif move == 'L':
pos[0]-=1
# y-axis
elif move == 'U':
pos[1]+=1
elif move == 'D':
pos[1]-=1

print("true" if pos == [0, 0] else "false")


""" Solution from 1012 """
Loading