Skip to main content

Posts

Showing posts with the label Programming Challenges

TIC TAC TOE GAME - Simulation of 2 Players

#!/usr/bin/python import random # TIC- TAC TOE GAME Simulation (2 Player , Random Positions) # X | O | X # ---------- # X | O | X # ---------- # O | X | X def fill_tic_tac(): #remove from the position list since this position is occupied #randomly select player and position mark on the matrix position = [0,1,2,3,4,5,6,7,8] box = [0]*(9) flag = True #Marking the Box for i in range(9): #Select Random Position in Matrix pos = random.choice(position) #Delete the position from original position position.remove(pos) #Select the Random player and Insert Player 1 -> 'X' ,Player 2 -> 'O' pl = random.randint(1,2) if(pl == 1): box[pos] = 'X' #Check_Winner here if(check_winner(box,'X')): flag = False print("*** Player 1 - X Is Winner *** ") break else: box[pos] = 'Y' #Check Winner Here if(check_winner(box,'Y')): flag = False print("...

Program to find sum of first 1000 Primes Numbers

#include <stdio.h> int main (int argc,char *argv[]) {         int i,j,sum = 0,range = 1000;         int flag = 0,count = 0;         for (i = 2 ;count < 1000;i++) {                 //check for prime                 for (j = 2; j < i ;j++) {                         if (i % j == 0) {                                 flag = 1;break;                       ...

Program for Largest Prime Palindrome (1 - 1000)

/* Program to print largest prime palindrome with range (1-1000)     Problem - https://www.codeeval.com/open_challenges/3/ */ #include <iostream> using namespace std; int main() {         int num,i,j,digit = 0,rev = 0;         int flag = 0;         for (i = 1000;i >= 2;i--) {                 flag = 0,rev = 0;                 //check for prime                 for ( j =2; j < i /2;j++) {                         if ( i % j == 0) {              ...

Solution Program for Fizz Buzz Problem

#include <stdio.h> #include <stdlib.h>   /*   * Solution Program for FizzBuzz (codeeval.com) - https://www.codeeval.com/open_challenges/1/   */ int main(int argc,char *argv[]) {         FILE *fp;         int x,y,count,i;         if (argc < 2) {          printf("Error :- File not specified \n");          exit(0);         }         fp = fopen (argv[1],"r");         if (fp == NULL) {                 printf("Error :- Can't open file \n");                 exit(0);         } ...

Program to print a non repeated character in string

#include<stdio.h> #include<string.h> /* Program to print a non repeated character in string  *  */ int main() {         int count[256] = {0}; /* count of the characters */         char string[20];         int i;         printf("Enter the string \n");         scanf("%s",string);         for (i = 0;i < strlen(string)-1;i++) {                 count[*(string+i)]++;   /* increments the count based on ASCII Range */         }         for (i = 0;i < strlen(string)-1;i++) {                 if (count[*(string+i)]== ...

C Program to Reverse a string

#include        <stdio.h> #include        <string.h> #include        <stdlib.h> /* Program - Reverse given string with poniters    Author - Surendra Patil */ int main() {         char *str,*rev,*ptr,temp;         str = (char *)malloc(10*sizeof(char));         printf("Enter the string \n");         scanf("%s",str);         printf("String before reversing = %s len = %d \n",str,strlen(str));         ptr = str       /* point to first character */;         rev = ptr + (strlen(ptr)-1); /* point to last character */         /* keep swappi...

C Program to print the decimal number in Binary using bit wise operators.

Algorithm:- Step 1. Read the decimal number in a variable              Read num Step 2. Loop from 31 bit position to 0 th bit              Loop i = 31 to 0 Step 3. Left shift 1 by the [i]th Index             mask = 1 << i Step 4. perform  ( & ) operation on the num and mask and print the results         if (num & mask == 0)                       print 0           else                       print 1 C Program:- #include<stdio.h>  /* program for binar...

Implementing strlen() function in c

Steps:- read a string from keyboard pass string to a function as pointer increment the string pointer and i till it encounters null ('\0') and while fails return the length to main program /* C program */ #include<stdio.h> #include<stdlib.h> /* program- calculate the lengh of the string (strlen()) Author:- Surendra Patil Date:- july 11 2013 */ int str_len(char *); int main() {   char *str = NULL;   /* allocate soutput:-ome memory */   str = (char*)malloc(20*sizeof(char));   printf("Enter the string \n");   scanf("%s",str);   printf("Length of the string = %d\n",str_len(str)); return 0; }  /* Function returns the length of the string */ int str_len (char *string) {   int i =0;   /* increment the pointer till end of the string */   while(*string++)    ...