Skip to content

Instantly share code, notes, and snippets.

@M-ZubairAhmed
M-ZubairAhmed / seconds to d,h,m,s.py
Last active April 1, 2016 11:40
This program splits the seconds you enter into Days:Hours:Minutes:Seconds
while True:
print("This program splits the seconds you enter into Days:Hours:Minutes:Seconds")
n = int(input("enter seconds"))
days = n // (24*60*60)
days_rem = n %(24*60*60)
hours = days_rem // (60*60)
hours_rem = days_rem % (60*60)
minutes = hours_rem//60
seconds = hours_rem%60
@M-ZubairAhmed
M-ZubairAhmed / 10s_triangle.py
Created April 1, 2016 14:25
Creates a right angled triangle with the user defined edge
n = int(input("enter triangle number"))
ini = 10
for x in range(1,n+1):
if (x==1):
print (1)
else:
ini = ini * 10
print (ini)
@M-ZubairAhmed
M-ZubairAhmed / left and right.py
Created April 8, 2016 14:04
Left and Right app by IBM made simple
import random
while True:
if (random.randint(1,2) == 1):
print("Right")
#print(random_number)
input("hit enter for next")
if (random.randint(1,2) == 2):
print("Left")
#print(random_number)
input("hit enter for next")
@M-ZubairAhmed
M-ZubairAhmed / conversionBinaryToDecimal.java
Created January 13, 2017 14:37
Conversion of a Binary Number to Decimal Number
public static int binaryToDecimal (int binaryNumber){
int digitIndex;
int decimalNum = 0;
int powerIndex = 0;
while(( binaryNumber != 0) && ( binaryNumber >0)){
digitIndex = binaryNumber % 10;
decimalNum = decimalNum + (int)(Math.pow(2,powerIndex) * digitIndex);
binaryNumber = binaryNumber / 10;
powerIndex++;
}
@M-ZubairAhmed
M-ZubairAhmed / conversionDecimalToBinaryHexaDecimal.java
Created January 13, 2017 14:41
Conversion from a Decimal Number to Binary & Hexadecimal Number
int decimalNum;
//Converts decimal number to binary, where regex is 2
Integer.toString(decimalNum,2);
//Converts decimal number to hexadecimal, where regex is 16
Integer.toString(decimalNum,16);
@M-ZubairAhmed
M-ZubairAhmed / removingRepetitions.java
Created January 25, 2017 07:38
A simple algorithm to remove repetitions form the string. eg. azzbyyyx -> abyx
public class Main {
public static void main(String[] args) {
String originalString = "azzbyyyx";
StringBuilder str = new StringBuilder(originalString);
int loopCounter = 0;
int repetitionCounter = 0;
while (true) {
if (loopCounter < str.length() - 1) {
@M-ZubairAhmed
M-ZubairAhmed / bitChange.java
Created January 25, 2017 13:25
Calculating Number of Bits changed to get to new Number
public static int convertBits(int initialNumber, int newNumber) {
return Integer.bitCount(initialNumber ^ newNumber);
}
}
@M-ZubairAhmed
M-ZubairAhmed / gist:8c6720c0e1537e3729b9fa3283c2eb23
Created February 8, 2017 13:53 — forked from gravitymonkey/gist:2406023
100 most common english words, as a java string array
String[] common = {"the","of","and","a","to","in","is","you","that","it","he","was","for","on","are","as","with","his","they","I","at","be","this","have","from","or","one","had","by","word","but","not","what","all","were","we","when","your","can","said","there","use","an","each","which","she","do","how","their","if","will","up","other","about","out","many","then","them","these","so","some","her","would","make","like","him","into","time","has","look","two","more","write","go","see","number","no","way","could","people","my","than","first","water","been","call","who","oil","its","now","find","long","down","day","did","get","come","made","may","part"};
@M-ZubairAhmed
M-ZubairAhmed / HugeFactorials.java
Created February 13, 2017 07:27
Calculating factorial of huge numbers using BigInteger Java class.
//bNum = Initializing big interger to value.
BigInteger bNum = BigInteger.ONE;
// n = The input number whose factorial is to be calculated.
for (long i = 1; i <= n; i++) {
bNum = bNum.multiply(bNum.valueOf(i));
}
//Storing the value of factorial in a string.
String factorialValue = String.valueOf(bNum);
@M-ZubairAhmed
M-ZubairAhmed / Factorials Trailing zeros Count
Last active February 13, 2017 08:32
Calculating a factorials trailing zeroes
/* When the given problem is to find number number of trailing zeros of the factorial, one is not required to
find out the factorial.
Statement : factorial(5) = 120 = 1*2*3*4*5
factorial(4) = 24 = 1*2*3*4
if it is observed the occurance of 5 or multiples of five is in direct proportion to number of trailing zeros
If however, expression contains powers of 5, then the trailing zeros increasing with power of 5.
factorial(25) = 1*...5*..10..*15*..20*..25
As expected number of trailing zero in above is expected to be 5, but since 25 is 5 power 2, it adds another zero
hence final count is 6.
*/