Skip to content

Instantly share code, notes, and snippets.

@kballenegger
Last active January 3, 2016 04:49
Show Gist options
  • Select an option

  • Save kballenegger/8411796 to your computer and use it in GitHub Desktop.

Select an option

Save kballenegger/8411796 to your computer and use it in GitHub Desktop.
//
// File.c
//
//
// Created by Kenneth Ballenegger on 1/13/14.
//
//
#include <stdlib.h>
#include <stdio.h>
typedef struct node_t *node_p;
typedef struct node_t {
int value;
node_p next;
} node_t;
node_p new_node(int value, node_p next) {
node_p node = calloc(1, sizeof(node_t));
node->value = value;
node->next = next;
return node;
}
// i'm leaking memory, i don't care. fuck it
int middle_node_int_rec(node_p next, int count_so_far, node_p *output);
int middle_node_int(node_p node) {
node_p middle_node = NULL;
middle_node_int_rec(node, 0, &middle_node);
return middle_node->value;
}
// note: does not account for lists with an even number of node, handled by int division
// used internally only, returns max
int middle_node_int_rec(node_p next, int count_so_far, node_p *output) {
printf("testing node with int %d\n", next->value);
if (next->next) {
int max = middle_node_int_rec(next->next, count_so_far + 1, output);
if (count_so_far == max / 2) {
*output = next;
}
return max;
} else {
return count_so_far;
}
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
// creating nodes, from last to first
node_p node;
node = new_node(9, NULL);
node = new_node(8, node);
node = new_node(7, node);
node = new_node(6, node);
node = new_node(5, node);
node = new_node(4, node);
node = new_node(3, node);
node = new_node(2, node);
node = new_node(1, node);
printf("result: %d", middle_node_int(node));
return 0;
}
# Enter your code here. Read input from STDIN. Print output to STDOUT
def min_greater_palin(input)
string_of_in = input.to_s
half = (string_of_in.length / 2).to_i # this takes care of the non-even case
first_half = string_of_in[0..(half-1)]
even = string_of_in.length.even?
even_offset = even ? 0 : 1
last_half = string_of_in[(string_of_in.length-half)..(string_of_in.length-1+even_offset)]
first_half_int = first_half.to_i
last_half_int = last_half.to_i
reversed_first_half = first_half.reverse
reversed_first_half_int = reversed_first_half.to_i
delta = reversed_first_half_int - last_half_int
return delta if delta > 0
multiple = even ? 1 : 10
total_added = 10 ** half * multiple
added_reversed_first_half_int = reversed_first_half_int + total_added
added_reversed_first_half_int - last_half_int + (10 ** (half-1))
end
STDIN.read.split("\n").each do |l|
puts min_greater_palin((l.to_f * 100).to_i).to_f / 100 # assume that l is an string of an float, and only has two decimal points
end
# Enter your code here. Read input from STDIN. Print output to STDOUT
input = STDIN.read.gsub(/[^0-9,]/,'').split(',').map(&:to_i)
cur = 0
while true
break if cur == input.length - 1
a = input[cur]
b = input[cur+1]
if a > b
input[cur] = b
input[cur+1] = a
cur = cur - 1
cur = 0 if cur < 0
else
cur = cur + 1
end
end
p input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment