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

Day 5 #585

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Day 5 #585

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
96 changes: 96 additions & 0 deletions Abhay/Day 5 program1
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
Import java.util.Arrays;

class Date implements Comparable {
private int day, month;
public Date(String str) {
String[] parts = str.split("/");
int x = Integer.parseInt(parts[0]);
int y = Integer.parseInt(parts[1]);
if (x > y) {
day = x; month = y;
}
else {
day = y; month = x;
}
}

public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public void swap() {
int temp = day; day = month; month = temp;
}

public int compareTo(Object o) {
Date other = (Date) o;
if (month < other.month) {
return -1;
}
if (month == other.month) {
if (day < other.day) {
return -1;
}
if (day == other.day) {
return 0;
}
return 1;
}
return 1;
}

private static String normalize(int n) {
StringBuffer sb = new StringBuffer();
if (n < 10) {
sb.append('0');
}
sb.append(n);
return sb.toString();
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(normalize(month));
sb.append('/');
sb.append(normalize(day));
return sb.toString();
}
}

public class DateFormat {
public String join(String[] a) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < a.length; ++i) {
sb.append(a[i]);
}
return sb.toString();
}
public String fromEuropeanToUs(String[] dateList) {
String[] tokens = join(dateList).split(" ");

Date[] dates = new Date[tokens.length];
dates[0] = new Date(tokens[0]);

for (int i = 1; i < tokens.length; ++i) {
dates[i] = new Date(tokens[i]);
if (dates[i].compareTo(dates[i - 1]) <= 0) {
if (dates[i].getDay() > 12) {
return "";
}
dates[i].swap();
if (dates[i].compareTo(dates[i - 1]) <= 0) {
return "";
}
}
}

StringBuffer sb = new StringBuffer();
sb.append(dates[0]).toString();
for (int i = 1; i < dates.length; ++i) {
sb.append(' ');
sb.append(dates[i].toString());
}
return sb.toString();
}
}
15 changes: 15 additions & 0 deletions Abhay/Day5 program2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
2. public class LameKnight {
public int maxCells(int height,
int width) {
if (height == 1 || width == 1) {
return 1;
}
if (height == 2) {
return Math.min(4, (width + 1)/2);
}
if (width >= 7) {
return width - 2;
}
return Math.min(4, width);
}
}
54 changes: 54 additions & 0 deletions Abhay/Day5 program3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <string>
#include <iostream>
#include <math.h>
using namespace std;

class Arrows{
public:
bool isAnArrow(string substring){
int length = substring.length();
if(substring[0] == '<'){
for(int i = 1; i<length; ++i){
for(int j=1; j<length; ++j){
if (substring[i] != substring[j]){
return false;
}
if ((substring[i] != '-') && (substring[i] != '=')){
return false;
}
}
}
return true;
}
else if(substring[length-1] == '>'){
for(int i = 0; i<length-1; ++i){
for(int j=0; j<length-1; ++j){
if (substring[i] != substring[j]){
return false;
} else if ((substring[i] != '-') && (substring[i]!='=')){
return false;
}
}
}
return true;
} else {
return false;
}

}
int longestArrow(string s){
int answer = -1;
string substring;
int length = s.length();
//The following for loops are meant to generate every substring
for (int i=0; i<length; i++){
for (int j=0; j<=length-i; j++){
substring = s.substr(i,j);
if(isAnArrow(substring)){
answer = max(answer, (int) substring.length());
}
}
}
return answer;
}
};
14 changes: 14 additions & 0 deletions Abhay/Day5 program4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ABBA(object):
def canObtain(self, initial, target):
if len(initial) == len(target):
if initial == target:
return 'Possible'
else:
return 'Impossible'
else:
result_op_1 = self.canObtain(initial + 'A', target)
result_op_2 = self.canObtain(initial[::-1] + 'B', target)
if result_op_1 == 'Possible':
return result_op_1
else:
return result_op_2
21 changes: 21 additions & 0 deletions Abhay/day5 program5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class LuckyRemainder {
public int pow2mod9(int n) {
switch (n % 6) {
case 0 : return 1;
case 1 : return 2;
case 2 : return 4;
case 3 : return 8;
case 4 : return 7;
case 5 : return 5;
}
return 0;
}
public int getLuckyRemainder(String X) {
int sum = 0;
for (int i = 0; i < X.length(); ++i) {
sum += X.charAt(i) - '0';
}
sum %= 9;
return (sum * pow2mod9(X.length() - 1)) % 9;
}
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Name: Abhay