Thursday, September 20, 2012

Redirecting standard input and standard output


Redirecting input to stdin from a file -
./a.out < in.txt

Redirecting input from stdout to a file -
./a.out > out.txt

Combining both -
./a.out < in.txt > out.txt

Friday, September 14, 2012

Sum of digits of factorial of a number

This is another projecteuler.net problem. This is again, very simple and gives programmers a chance to practice any new language they are learning. I wrote it in Python :)

#Problem 20
#Sum of digits in factorial

def fact(n):
    m = int(n/5)
    r = n - (m*5)
    i = 1
    f = 1
    while m > 0:
        m = m - 1
        f = f * i
        i = i + 1
        f = f * i
        i = i + 1
        f = f * i
        i = i + 1
        f = f * i
        i = i + 1
        f = f * i
        i = i + 1
        f = f / 10
    while r > 0:
        r = r - 1
        f = f * i
        i = i + 1
    return f

def digsum(n):
    s = 0
    while n > 0:
        s = s + (n%10)
        n = n/10
    return s

if __name__ == "__main__":
    n = int(raw_input("Enter n : "))
    f = fact(n)
    s = digsum(f)
    print f, s

Read the code carefully, while it is not perfect (100% correct though), there are many things to learn for newbie.

Thursday, August 16, 2012

Python program to find the largest prime factor of the number

Recently I started learning Python. Having programmed in C, C++ and Java up to now, I found it rather different, but easy :). Here is my small and first ("Hello World" is too boring now!) program in Python-

#Computes the largest prime factor for the number
#Solution for Problem 3 on Project Euler.net

import math

x = int(raw_input('Enter Number : '))
max_factor = 2

i = (x+1)/2

while i > 1:
    if x % i == 0:
        j = 2
        while j <= int(math.sqrt(i)):
            if i % j == 0:
                break
            j = j + 1
        else:
            max_factor = i
            break
    i = i - 1

print max_factor


To find all the prime factors just use a list instead of max_factor, and keep appending "i" to it. Also remove the 'break' statement in 'else:'.

Wednesday, August 15, 2012

Reverse a linked list in group of k

This is one of a candidate interview question. I don't see any practical application of this-
Given a linked list, reverse the list in groups of size k. 
I am sure it is not clear to you, so here is a example-
Input -    1=>2=>3=>4=>5=>6=>7=>8
Output -  3=>2=>1=>6=>5=>4=>8=>7

Try solving it yourself before going through the code.
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
int v;
struct node *next;
} node;

node * revk(node *head, int k)
{
int i = 1;
node *p, *b, *s1, *s2, *t;
p = head;
b = NULL;
s1 = NULL;
s2 = head;

while (p)
{
t = p;
p = p->next;
if (t)
t->next = b;
b = t;

if (i == k)
{
if (s1)
s1->next = b;
else
head = b;
s1 = s2;
s2 = p;
i = 0;
}
i++;
}
s1->next = b;
s2->next = NULL;
return head;
}

void print(node *head)
{
node *p = head;
while (p)
{
printf("%d -> ", p->v);
p = p->next;
}
printf("$\n");
}

void insert(node **t, int v)
{
node *p = malloc(sizeof(node));
p->v = v;
p->next = NULL;
(*t)->next = p;
*t = p;
}

int main(int argc, char *argv[])
{
node *head, *tail = NULL;
head = malloc(sizeof(node));
head->next = NULL;
head->v = 1;
tail = head;
insert(&tail, 2);
insert(&tail, 3);
insert(&tail, 4);
insert(&tail, 5);
insert(&tail, 6);
insert(&tail, 7);
insert(&tail, 8);

print(head);

head = revk(head, 3);

print(head);

tail = head;

while (tail)
{
head = tail;
tail = tail->next;
free(head);
}

printf("\n");
return 0;
}