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

Andrei negura homework2 #129

Open
wants to merge 14 commits into
base: master
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
101 changes: 0 additions & 101 deletions src/Homework1.java

This file was deleted.

20 changes: 20 additions & 0 deletions src/Homework1/ex10_BooleanStringContainsAnotherString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package Homework1;

public class ex10_BooleanStringContainsAnotherString {
public static void main(String[] args) {
String s1 = "Hello java world";
String s2 = "java";

if(s1.contains(s2)){
System.out.println("TRUE");
}else
System.out.println("FALSE");
}
}

/*
Write a program that test if a given String contains another String.

For example
Given the following base String “Hello java world” and the target test String “java” the output should be TRUE
*/
22 changes: 22 additions & 0 deletions src/Homework1/ex11_CountCharacterInAString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package Homework1;

public class ex11_CountCharacterInAString {
public static void main(String[] args) {
String s = "Hello java world";
char target = 'l';
int count = 0;

for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == target) {
count++;
}
}
System.out.println(count);
}
}
/*
Write a program that will count how many times a character appears in a String
For example
Given the string “Hello java world” and the target character ‘a’ the output should be 2
(since ‘a’ appears twice in “Hello java world”)
*/
27 changes: 27 additions & 0 deletions src/Homework1/ex12_BooleanStringEndsWithString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package Homework1;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

public class ex12_BooleanStringEndsWithString {
public static void main(String[] args) {
String sir1 = "Hello java world";
String sir2 = "world";

System.out.println("Sirul " + sir1 + " se termina cu " + sir2 + " : " + isSufix(sir1, sir2));
}
private static boolean isSufix(String sir1, String sir2) {
String[] words = sir1.split(" ");

if (words[words.length - 1].equals(sir2))
return true;
else
return false;
}
}
/* Write a program that will test whether a string ends with another string

For example
Given the following strings “Hello java world” and “world” the output should be TRUE

*/
64 changes: 64 additions & 0 deletions src/Homework1/ex13_ReturnDifferentString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package Homework1;

import java.util.HashMap;
import java.util.Map;

public class ex13_ReturnDifferentString {
public static void main(String[] args) {
String alphabet = "abcdefgh";
System.out.println(parse(alphabet));
}
// ex11_CountCharacterInAString letter = alphabet(0);
// System.out.println(result);
public static String parse(String alphabet) {
Map<String, Integer> map = new HashMap<String, Integer>();
//Adding elements to map
map.put("a", 0);
map.put("b", 1);
map.put("c", 2);
map.put("d", 3);
map.put("e", 4);
map.put("f", 5);
map.put("g", 6);
map.put("h", 7);
map.put("i", 8);
map.put("j", 9);
map.put("k", 10);
map.put("l", 11);
map.put("m", 12);
map.put("n", 13);
map.put("o", 14);
map.put("p", 15);
map.put("q", 16);
map.put("r", 17);
map.put("s", 18);
map.put("t", 19);
map.put("u", 20);
map.put("v", 21);
map.put("w", 22);
map.put("x", 23);
map.put("y", 24);
map.put("z", 25);
String z = ""+ map.get("z");
String result = "";
// Set<String> keys = map.keySet();
// for (String k : keys) {
// System.out.println("Key: " + k)
for (int i = 0; i < alphabet.length(); i++) {
result += map.get(""+alphabet.charAt(i));
}
return result;
}
}
/*
Write a program that will return a different string representing the alphabet index of each letter.
Assuming we have the following alphabet encoding
a = 0
b = 1
c = 2
d = 3
For the given string “abcd” the output should be “0123”
*/


30 changes: 30 additions & 0 deletions src/Homework1/ex14_InsertSymbolsBetweenDuplicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package Homework1;
public class ex14_InsertSymbolsBetweenDuplicates {
public static void main(String[] args) {
String lett = "Hello worlld this is someething cooll";
String lett2 = "Hellllllo";
String insert = "-";
System.out.println(processString(lett,insert));
System.out.println(processString(lett2,"$"));
}
private static String processString(String s, String delimitator) {
StringBuilder rs = new StringBuilder();
if (s.length() > 1)
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == s.charAt(i + 1)) {
rs.append( s.substring(i, i + 1)).append(delimitator);
} else {
rs.append( s.substring(i, i + 1));
}
}
rs.append(s.substring(s.length() - 1));
return rs.toString();
}
}

/*
Write a program that will insert a # between any duplicate letters that are placed right next to each.
For example

Given the following string “Hello world” the output should be “Hel#lo world”
*/
39 changes: 39 additions & 0 deletions src/Homework1/ex15_Palindrom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package Homework1;

public class ex15_Palindrom {
public static void main(String[] args) {
String s1 = "ababa";
String s2 = "aba";
String s3 = "abba";
String s4 = "aaaabaaaa";
String s5 = "abbabb";
System.out.println("The provided string" + s1 + " is palindrome: " + isPalindrome(s1));
System.out.println("The provided string" + s2 + " is palindrome: " + isPalindrome(s2));
System.out.println("The provided string" + s3 + " is palindrome: " + isPalindrome(s3));
System.out.println("The provided string" + s4 + " is palindrome: " + isPalindrome(s4));
System.out.println("The provided string" + s5 + " is palindrome: " + isPalindrome(s5));
}

private static boolean isPalindrome(String s) {
// StringBuilder reversedString = new StringBuilder(s).reverse();
// return s.equals(reversedString.toString());
boolean isPalindrome = true;
int n = s.length() / 2;
int lenght = s.length() - 1;
for (int i = 0; i <= n; i++) {
if (s.charAt(i) != s.charAt(lenght - i)) {
isPalindrome = false;
break;
}
}
return isPalindrome;
}
}

/*
Write a program to test whether a given string is a palindrome or not

For example
Given the following string “aaabccbaaa” should return TRUE
Given the following string “abccbb” should return FALSE
*/
24 changes: 24 additions & 0 deletions src/Homework1/ex1_Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package Homework1;

public class ex1_Sum {
public static void main(String[] args) {
int[] ints = {1, 2, 3, 4, 5, 6, 7, 8};
// Calculate sum up all numbers from 0 up to a target number
// Target number is 6
/*
int sum = ints[0] + ints[1] + ints[3] + ints[4] + ints[5];
System.out.println("suma numerelor alese din intervalul mentionat este: " + sum);
*/

int sum = 0;
for (int i = 0; i < 6; i++) {
sum = sum + ints[i];
}
System.out.println("suma numerelor alese din intervalul mentionat este: " + sum);
}
}

/* Cerinta:
Write a program that will sum up all numbers from 0 up to a target number.
For example, for an input number 5, result should be 15 (0+1+2+3+4+5) */

28 changes: 28 additions & 0 deletions src/Homework1/ex2_AverageAndSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package Homework1;

public class ex2_AverageAndSum {
public static void main(String[] args) {

int a = 1;
int b = 100;
int sum = 0;

for (int i = a; i <= b; i++) {
sum = sum + i;
}
System.out.println("suma valorilor din intervalul " + a + " si " + b + " este: " + sum);

double avg = (a + b) / 2;

System.out.println("media este:" + avg);
}
}

/*
Cerinta:
Write a program that will calculate the average and sum of all numbers between the range of 2 numbers.
For example, for range numbers 1 to 100 output should be:
Sum for range 1 to 100 : 5050
Average for range 1 to 100 : 50.0
*/

Loading