Skip to content

Instantly share code, notes, and snippets.

@M-ZubairAhmed
M-ZubairAhmed / Josephus Survivor Position.java
Created March 22, 2017 15:30
This algorithm calculates safe position when Josephus circle game is played
import java.util.ArrayList;
public class JosephusPosition {
public static int josephusSurvivorPosition(final int n, final int k) {
// 'n' is the number of people in the cave of death, such that :- the first person is 1 and last is n
ArrayList soldiers = new ArrayList<>();
for (int j = 1; j <= n; j++) {
soldiers.add(j);
@M-ZubairAhmed
M-ZubairAhmed / Luhn Algorithm.java
Created March 22, 2017 15:25
This Algorithm verifies credit card numbers
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
long creditCardNo = 0;
Scanner scan = new Scanner(System.in);
@M-ZubairAhmed
M-ZubairAhmed / Vigenere Encryption.java
Created March 22, 2017 04:39
Algorithm to encrypt text with Vigenere method
public class Main {
public static void main(String[] args) {
//Entering given text for process.
System.out.println("Enter the text to be encrypted with Vigenere method");
Scanner scanInputString = new Scanner(System.in);
String inputString = scanInputString.nextLine().toLowerCase();
//Entering key word/s.
@M-ZubairAhmed
M-ZubairAhmed / New Caesar Decryption.java
Created March 22, 2017 04:33
Algorithm for decrypting improved version of Caesar cypher
public static String demovingShift(String inputCodedString, int shift, int decremental){
char[] inCodedCharArr = new char[inputCodedString.length()];
for (int q = 0; q < inputCodedString.length(); q++) {
int DeFlushV =0 , DeFlushVDec = 0;
DeFlushV = (int)inputCodedString.charAt(q);
if (DeFlushV >= 65 && DeFlushV <=90){
DeFlushVDec = DeFlushV - shift;
if(DeFlushVDec < 65) {
while (DeFlushVDec < 65) {
@M-ZubairAhmed
M-ZubairAhmed / New Caesar Encryption.java
Created March 22, 2017 04:30
Algorithm for improved version of Caesar encryption with variable key
/**
* This method is for encription of text.
* @param s The input text which is to be encrypted
* @param shift The initial key value to be executed at the first
* letter of the text.
* @param incremental The number by which the value of key is increased.
* @return The encrypted text is returned.
**/
public static String movingShift(String s, int shift, int incremental){
@M-ZubairAhmed
M-ZubairAhmed / Caesar Decryption.java
Created March 22, 2017 04:16
Caesar Decryption Algorithm
public class NotePassing {
public static String decipher (String codedMessage, int key){
char[] charArray = new char[codedMessage.length()];
int[] intArray = new int[codedMessage.length()+1];
for (int r = 0; r < codedMessage.length(); r++){
boolean isUCase = (codedMessage.charAt(r)>=65) && (codedMessage.charAt(r)<=90);
boolean isLCase = (codedMessage.charAt(r)>=97) && (codedMessage.charAt(r)<=122);
int changedAsci = ((int)codedMessage.charAt(r)) + key;
if (isUCase) {
@M-ZubairAhmed
M-ZubairAhmed / keypadTaps.java
Created March 17, 2017 05:27
Calculates number of taps required to type a text in old numpad keyboard phones
public class Keypad {
public static int presses(String phrase){
int noPresses = 0;
for(int i = 0 ; i<phrase.length(); i++){
char aToZ = phrase.toUpperCase().charAt(i);
if (aToZ == 'A'||aToZ == 'D'||aToZ == 'G'||aToZ == 'J'||aToZ == 'M'||aToZ == 'P'||aToZ == 'T'||aToZ == 'W'||aToZ == '1'||aToZ == ' '||aToZ == '*'||aToZ == '#'){
noPresses = noPresses + 1;
System.out.println(noPresses+" 1 = "+aToZ);}
else if (aToZ == 'B'||aToZ == 'E'||aToZ == 'H'||aToZ == 'K'||aToZ == 'N'||aToZ == 'Q'||aToZ == 'U'||aToZ == 'X'||aToZ == '0'){
@M-ZubairAhmed
M-ZubairAhmed / stammeringPattern.java
Created March 17, 2017 05:23
Pattern generating algorithm. eg: abcd -> A-Bb-Ccc-Dddd
public class Accumul {
public static String accum(String s){
StringBuilder stBuild = new StringBuilder();
for(int i = 0; i < s.length(); i++){
for(int j = 0; j <= i; j++) {
if (j==0){
stBuild.append(s.toUpperCase().charAt(i));
}
else {
stBuild.append(s.toLowerCase().charAt(i));
@M-ZubairAhmed
M-ZubairAhmed / isHex.java
Created March 17, 2017 05:19
Algorithm to check if agiven is a valid hexadecimal number.
public class StringUtils {
public static boolean isHexNumber(String s) {
int cursor;
if (s.length() == 0){
return false;
}
@M-ZubairAhmed
M-ZubairAhmed / Android Studio .gitignore
Created February 23, 2017 07:19 — forked from iainconnor/Android Studio .gitignore
A .gitignore for use in Android Studio
# Built application files
/*/build/
# Crashlytics configuations
com_crashlytics_export_strings.xml
# Local configuration file (sdk path, etc)
local.properties
# Gradle generated files