Skip to content

Instantly share code, notes, and snippets.

@tcelik
Created April 17, 2019 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcelik/a7ad85f2ed9b925b9e81e8fee80344ef to your computer and use it in GitHub Desktop.
Save tcelik/a7ad85f2ed9b925b9e81e8fee80344ef to your computer and use it in GitHub Desktop.
Argela Mülakatı Deneyimi
package com.argela.sampleapp.app;
/*
Program: Write all occurences, how many each character in given string?
Sample Case
---
Input : "Araba"
Output: A:1
r:1
a:2
b:1
*/
import java.util.HashMap;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class App {
public static void main(String[] args)
{
// Aynı problemi 4 yolla implemente ettik.
allOccurencesProblemV1("Araba");
System.out.println("****");
allOccurencesProblemV2("Araba");
System.out.println("****");
allOccurencesProblemV3("Araba");
System.out.println("****");
allOccurencesProblemV4("Araba");
System.out.println("****");
}
// Programlamaya Giriş Edasıyla Çözmek
public static void allOccurencesProblemV1(String input)
{
// array alınır
char [] chars = input.toCharArray();
for (int i = 0; i < chars.length; ++i) {
char ch = chars[i];
int count = 0;
for (int j = 0; j < chars.length; ++j) {
if (chars[j] == ch)
count++;
}
System.out.printf("%c:%d%n", ch, count);
}
}
// Geleneksel count array ile çözmek
public static void allOccurencesProblemV2(String input)
{
// Önce bir güzel char dizisi alınır
char [] chars = input.toCharArray();
// Bir tutam count dizisi alınır
int [] count = new int['z' - 'A' + 1];
// Dolaşıp, dizide sayma yapılır.
for (char ch : chars) {
count[ch - 'A']++;
}
// İstenirse ekrana basılır
for (int i = 0; i < count.length; ++i) {
if (count[i] != 0) {
System.out.printf("%c:%d%n", (char) i + 'A', count[i]);
}
}
}
// Collection Bilen Bir Edayla Çözmek
public static void allOccurencesProblemV3(String input)
{
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
char [] chars = input.toCharArray();
for (char ch : chars) {
if (hm.containsKey(ch))
hm.put(ch, hm.get(ch) + 1);
else
hm.put(ch, 1);
}
// Look at the hash map.
System.out.println(hm);
}
// Lambda ifadelerini bilen bir edayla çözmek (Java8 ile derle çalıştır)
public static void allOccurencesProblemV4(String input)
{
// Bir güzel Stream elde edilir.
IntStream intStream = input.chars();
// Int To Character Stream : Aslında A 65 olduğu için bu şekilde bir yaklaşım var. Map yapmak yeni bir stream.
Stream<Character> streamCharacter = intStream.mapToObj(val -> (char) val);
// HashMap alınır
HashMap<Character, Integer> hm = new HashMap<>();
// Stream dolaşmak ve HashMap veri yapısında saymak, hm yakaladı.
streamCharacter.forEach(ch -> {
if (hm.containsKey(ch)) hm.put(ch, hm.get(ch) + 1);
else hm.put(ch, 1);
});
// Look at the hash map.
System.out.println(hm);
}
}
@tcelik
Copy link
Author

tcelik commented Apr 17, 2019

java -version : 1.8 dönmeli en az. Yani java compiler 8 ile derlenip java 8 runtime ile çalışmalı. Lambda ifadesi ara kodda değişiklik yaptımı bilmiyorum. Ama derlerken kesinlikle java 8 compiler ile derlenmeli yoksa parse edemez.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment