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

Solved For Loops #6

Open
wants to merge 3 commits 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
57 changes: 57 additions & 0 deletions C++/Easy/ForLoop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
// Complete the code.
int num1, num2;
cin >> num1 >> num2;
for (int i = num1; i <= num2; i++)
{
if(i == 1)
{
cout <<"one" <<endl;
}
else if (i == 2)
{
cout <<"two" <<endl;
}
else if (i == 3)
{
cout <<"three" <<endl;
}
else if (i == 4)
{
cout <<"four" <<endl;
}
else if (i == 5)
{
cout <<"five" <<endl;
}
else if (i == 6)
{
cout <<"six" <<endl;
}
else if (i == 7)
{
cout <<"seven" <<endl;
}
else if (i == 8)
{
cout <<"eight" <<endl;
}
else if (i == 9)
{
cout <<"nine" <<endl;
}
else if (i%2 == 0)
{
cout <<"even" <<endl;
}
else if (i%2 != 0)
{
cout <<"odd" <<endl;
}
}
return 0;
}
34 changes: 34 additions & 0 deletions C++/Medium/3DSurfaceArea.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Read input values
height, width = map(int, input().split())
board = []
for _ in range(height):
row = list(map(int, input().split()))
board.append(row)

# Initialize the total surface area
surface_area = 0

# Iterate through the board and calculate the surface area for each cell
for i in range(height):
for j in range(width):
cell_height = board[i][j]

# Calculate the top and bottom areas
top_bottom_area = 2 if cell_height > 0 else 0

# Calculate the side areas by checking adjacent cells
left_height = 0 if j == 0 else board[i][j - 1]
right_height = 0 if j == width - 1 else board[i][j + 1]
up_height = 0 if i == 0 else board[i - 1][j]
down_height = 0 if i == height - 1 else board[i + 1][j]

side_area = max(0, cell_height - left_height) + max(0, cell_height - right_height) + max(0, cell_height - up_height) + max(0, cell_height - down_height)

# Add the calculated areas to the total surface area
surface_area += top_bottom_area + side_area

# Calculate the total price of the toy
price = surface_area

# Print the price of the toy
print(price)
31 changes: 31 additions & 0 deletions Python/Medium/BiggerisGreater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def biggerIsGreater(w):
w = list(w) # Convert the string to a list for easy character manipulation
i = len(w) - 2

# Find the first character w[i] such that w[i] < w[i+1]
while i >= 0 and w[i] >= w[i + 1]:
i -= 1

if i == -1:
return "no answer" # The entire string is non-increasing

# Find the rightmost character w[j] to the right of w[i] such that w[j] > w[i]
j = len(w) - 1
while w[j] <= w[i]:
j -= 1

# Swap w[i] and w[j]
w[i], w[j] = w[j], w[i]

# Reverse the substring to the right of w[i]
w[i + 1:] = reversed(w[i + 1:])

return ''.join(w)

# Read the number of test cases
t = int(input().strip())

for _ in range(t):
w = input().strip()
result = biggerIsGreater(w)
print(result)