Skip to content

Instantly share code, notes, and snippets.

@vipulshah2010
Last active October 17, 2021 13:28
Show Gist options
  • Save vipulshah2010/eef3ba5f2d9634ec8dffdd9f32b97dd9 to your computer and use it in GitHub Desktop.
Save vipulshah2010/eef3ba5f2d9634ec8dffdd9f32b97dd9 to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
System.out.println(parseEmoticons("How is it going? :) You are almost done :-)!"));
}
public static List<InlinedEmoticon> parseEmoticons(String text) {
if (text == null || text.length() == 0) {
return new ArrayList<>();
}
Map<String, Integer> map = new TreeMap<>();
map.put(":)", 1);
map.put(":-)", 1);
map.put(":/", 2);
map.put(":(", 3);
map.put(":-(", 3);
List<InlinedEmoticon> emoticons = new ArrayList<>();
for (String emotion : map.keySet()) {
int index = text.indexOf(emotion);
while (index >= 0) {
emoticons.add(new InlinedEmoticon(map.get(emotion), text.indexOf(emotion), emotion.length()));
index = text.indexOf(emotion, index + emotion.length());
}
}
return emoticons;
}
public static class InlinedEmoticon {
final int emoticonId;
final int position;
final int length;
public InlinedEmoticon(int emoticonId, int position, int length) {
this.emoticonId = emoticonId;
this.position = position;
this.length = length;
}
@Override
public String toString() {
return "[" +
emoticonId + "," +
position + "," +
length +
']';
}
}
public static void main(String[] args) {
System.out.println("" + findMissingLog(new ArrayList<Integer>() {
{
add(1);
add(2);
add(3);
add(4);
add(6);
}
}));
}
public static int findMissingLog(List<Integer> numbers) {
int sum = 0;
for (Integer number : numbers) {
sum += number;
}
int length = numbers.size() + 1;
int desiredSum = length * (length + 1) / 2;
return desiredSum - sum;
}
public static void main(String[] args) {
System.out.println("" + maxSum(new int[]{10, 20, 1, 5}));
}
static int maxSum(int[] list) {
int prev_prev_sum = 0;
int prev_sum = list[0];
int i, current_sum;
for (i = 1; i < list.length; i++) {
current_sum = prev_prev_sum + list[i];
prev_prev_sum = Math.max(prev_prev_sum, prev_sum);
prev_sum = current_sum;
}
return (Math.max(prev_prev_sum, prev_sum));
}
public static void main(String[] args) {
System.out.println("Rounded Price " + roundPrice(38, 1));
}
public static long roundPrice(int price, int n) {
int value = (int) Math.pow(10, n);
// Smaller multiple
int a = (price / value) * value;
// Larger multiple
int b = a + value;
// Return of closest of two
return (price - a > b - price) ? b : a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment