Structure vs Union in C – Coded Approach

Hello Everyone, Today we are going to talk a bit about the difference between structure and union as in C programming language. We are going to provide a coded approach so that you could see the difference practically as the theoretic differences you can read from anywhere on the internet and your text books. Here ...

Add Two Integers without using Arithmetic Operators

Recently came across an interesting question of adding two numbers without using any of the arithmetic operators and thought of giving it a try. The first thought of solving the question was using bit-manipulation operations. The idea was to add the numbers bit-by-bit taking any carry forward (without actually adding them and instead using bit-wise ...

Lexicographically Bigger String

The following piece of code accepts as input a single string of characters and produces on standard output a string which would be the next string to appear in lexicographical order. Definition of Lexicographical order : wiki (Also known as The Dictionary order since lexicographical order is one in which the strings would appear in ...

Check if a number is Fibonacci

A Fibonacci number is one which appears in the Fibonacci Sequence. To check if a given number n occurs in the Fibonacci Sequence or not we only need to check if one or both of (5*n^2 + 4) or (5*n^2 – 4) is a perfect square or not (source: wiki). Python code for the same: ...

Pascal Triangle in C

Pascal Triangle in C
The following C code generates on standard output a Pascal Triangle as specified on this wiki page. Specifically output in following format is generated: C /* The following code is used to print the Pascal triangle for as many rows as input by the user. */ #include<stdio.h> // begin the main function for this program ...

Code Jam Cookie Clicker Alpha

This is a solution to the Code Jam 2014 Qualification Round problem B, Cookie Clicker Alpha. (Problem Description Link) Python __author__ = 'Coddicted' def read_case(inf): return inf.readline().split(' ') def write_case(f, i, res): f.write('Case #%d: '%i) f.write('%.7f'%res) f.write('\n') def main(): with open('data_files/B-large-practice.in', 'r') as inf, open('data_files/B-large-practice.out', 'w') as of: t = int(inf.readline()) case_cnt = 1 while ...

Code Jam Magic Trick

Code Jam Magic Trick
This is the solution to Code Jam 2014 qualification round problem A. Magic Trick. (Problem Link) Python __author__ = 'Coddicted' def read_row(inf):     return inf.readline().strip().split(' ') def main():     with open('data_files/A−small−practice.in', 'r') as inf, open('data_files/A−small−practice.out', 'w') as of:         t = int(inf.readline())         case_cnt = 1         while case_cnt <= t:             row = int(inf.readline())             i = 1             row1 = []             while i <= 4:                 if i == row:                     row1 = read_row(inf)                 else:                     inf.readline()                 i += 1             row = int(inf.readline())             row2 = []             i = 1             while i <= 4:                 if i == row:                     row2 = read_row(inf)                 else:                     inf.readline()                 i += 1             row1 = [int(item) for item in row1]             row2 = [int(item) for item in row2]             comm = [item for item in row1 if item in row2]             if len(comm) == 1:                 of.write('Case #' + str(case_cnt) + ': ' + str(comm[0]) + '\n')                 print 'Case #' + str(case_cnt) + ': ' + str(comm[0])             elif len(comm) > 1:                 of.write('Case #' + str(case_cnt) + ': ' + 'Bad magician!' + '\n')                 print 'Case #' + str(case_cnt) + ': ' + 'Bad magician!'             else:                 of.write('Case #' + str(case_cnt) + ': ' + 'Volunteer cheated!' + '\n')                 print 'Case #' + str(case_cnt) + ': ' + 'Volunteer cheated!' ...

To compare two folders (recursively) in Java 21

Java package com.example; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Set; public class Compare { //This can be any folder locations which you want to compare File dir1 = new File("C:\\ComparisonFolder\\master"); File dir2 = new File("C:\\ComparisonFolder\\release"); public static void main(String ...args) { Compare compare = ...

Count Triangles in a Graph 1

The following code implementation counts the number of triangles present in a graph G. For graph representation JUNG API is used but can be extended/ changed to use any other notation by simple means. Java import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import edu.uci.ics.jung.graph.Graph; public class TriangleFinder { private Graph<Integer, String> g; /** * @return The ...

Dijkstra’s Algorithm : Finding all possible shortest paths between two vertices in a graph 4

Dijkstra's Algorithm : Finding all possible shortest paths between two vertices in a graph
The following code implements the Dijkstra’s Shortest Path Algorithm and further extends is to get all possible shortest paths between two vertices. For graph representation we make use of JUNG API, its usage, however, is primarily in visualizing the graph and can be extended easily for any other representation as well. Random Graph Generator code ...