Last active
March 26, 2023 12:57
-
-
Save sametaybaz/8c86599b9e3e870243053a5639fbb9f5 to your computer and use it in GitHub Desktop.
Coderbyte Algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Coderbyte Questions and Answers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First Factorial | |
Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4, then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer. | |
Examples | |
Input: 4 | |
Output: 24 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
10/10 | |
class Main { | |
public static int FirstFactorial(int num) { | |
int result; | |
result = 1; | |
while (num >= 1) | |
{ | |
result = result * num; | |
num--; | |
} | |
return result; | |
} | |
public static void main (String[] args) { | |
// keep this function call here | |
Scanner s = new Scanner(System.in); | |
System.out.print(FirstFactorial(s.nextLine())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First Reverse | |
Have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order. For example: if the input string is "Hello World and Coders" then your program should return the string sredoC dna dlroW olleH. | |
Examples | |
Input: "coderbyte" | |
Output: etybredoc | |
Input: "I Love Code" | |
Output: edoC evoL I |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LONG SOLUTION (BASED ECOLE 42) | |
// ****************************** | |
import java.util.*; | |
import java.io.*; | |
class Main { | |
public static String FirstReverse(String str) { | |
char[] charArray = str.toCharArray(); | |
char[] reversedArray = new char[charArray.length]; | |
int i = charArray.length - 1; | |
int j = 0; | |
while (i >= 0) { | |
reversedArray[j] = charArray[i]; | |
i--; | |
j++; | |
} | |
String reversedStr = new String(reversedArray); | |
return reversedStr; | |
} | |
public static void main(String[] args) { | |
Scanner s = new Scanner(System.in); | |
System.out.print(FirstReverse(s.nextLine())); | |
} | |
} | |
// SHORT SOLUTION | |
// ************** | |
10/10 | |
import java.util.*; | |
import java.io.*; | |
class Main { | |
public static String FirstReverse(String str) { | |
StringBuilder builder_str = new StringBuilder(str); | |
String reversed_str = builder_str.reverse().toString(); | |
return reversed_str; | |
// or make in one line ; | |
// return new StringBuilder(str).reverse().toString() | |
} | |
public static void main (String[] args) { | |
// keep this function call here | |
Scanner s = new Scanner(System.in); | |
System.out.print(FirstReverse(s.nextLine())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Longest Word | |
Have the function LongestWord(sen) take the sen parameter being passed and return the longest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty. Words may also contain numbers, for example "Hello world123 567" | |
Examples | |
Input: "fun&!! time" | |
Output: time | |
Input: "I love dogs" | |
Output: love |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
10/10 | |
class Main { | |
public static String LongestWord(String sen) { | |
// input => "fun123&!! time" | |
String sen_replaced = sen.replaceAll("[^a-zA-Z0-9 ]", ""); // "fun123 time" | |
String[] words = sen_replaced.split(" "); // {"fun123","time"} | |
String longestWord = ""; // define ="" for wont take error NullPointerException | |
for (String word : words) { | |
if (word.length() > longestWord.length()) { | |
longestWord = word; // fun123 | |
} | |
} | |
return longestWord; | |
} | |
public static void main (String[] args) { | |
// keep this function call here | |
Scanner s = new Scanner(System.in); | |
System.out.print(LongestWord(s.nextLine())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Questions Marks | |
Have the function QuestionsMarks(str) take the str string parameter, which will contain single digit numbers, letters, and question marks, and check if there are exactly 3 question marks between every pair of two numbers that add up to 10. If so, then your program should return the string true, otherwise it should return the string false. If there aren't any two numbers that add up to 10 in the string, then your program should return false as well. | |
For example: if str is "arrb6???4xxbl5???eee5" then your program should return true because there are exactly 3 question marks between 6 and 4, and 3 question marks between 5 and 5 at the end of the string. | |
Examples | |
Input: "aa6?9" | |
Output: false | |
Input: "acc?7??sss?3rr1??????5" | |
Output: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
import java.util.regex.*; | |
10/10 | |
class Main { | |
public static String QuestionsMarks(String str) { | |
str = str.replaceAll("[a-z]", ""); | |
Pattern pattern = Pattern.compile("([0-9])([?])([?])([?])([0-9])"); | |
Pattern falseP1 = Pattern.compile("([0-9])([?])([?])([0-9])"); | |
Pattern falseP2 = Pattern.compile("([0-9])([?])([0-9])"); | |
Matcher falseMatchP1 = falseP1.matcher(str); | |
Matcher falseMatchP2 = falseP2.matcher(str); | |
Matcher matchPattern = pattern.matcher(str); | |
if (falseMatchP1.find() || falseMatchP2.find()){ | |
return "false"; | |
} | |
if (matchPattern.find()){ | |
return "true"; | |
} | |
return "false"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Find Intersection | |
Have the function FindIntersection(strArr) read the array of strings stored in strArr which will contain 2 elements: the first element will represent a list of comma-separated numbers sorted in ascending order, the second element will represent a second list of comma-separated numbers (also sorted). Your goal is to return a comma-separated string containing the numbers that occur in elements of strArr in sorted order. If there is no intersection, return the string false. | |
Examples | |
Input: new String[] {"1, 3, 4, 7, 13", "1, 2, 4, 13, 15"} | |
Output: 1,4,13 | |
Input: new String[] {"1, 3, 9, 10, 17, 18", "1, 4, 9, 10"} | |
Output: 1,9,10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
10/10 | |
class Main { | |
public static String FindIntersection(String[] strArr) { | |
String str1 = strArr[0]; | |
String str2 = strArr[1]; | |
String result = ""; | |
String[] str1_arr = str1.split(", "); | |
String[] str2_arr = str2.split(", "); | |
for(String num_1: str1_arr) | |
{ | |
for(String num_2: str2_arr) | |
{ | |
if(num_1.equals(num_2)) // note = ! you can't num_1 == num_2 , it return false because they arent located in same memory area . | |
{ | |
if(result.length()==0) | |
result+=num_1; | |
else | |
result+=","+num_1; | |
} | |
} | |
} | |
if(result.isEmpty()) | |
return "false"; | |
return result; | |
} | |
public static void main (String[] args) { | |
// keep this function call here | |
Scanner s = new Scanner(System.in); | |
System.out.print(FindIntersection(s.nextLine())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment