Skip to content

Instantly share code, notes, and snippets.

@PatchRowcester
PatchRowcester / AssignmentOperators.java
Created January 2, 2013 10:20
Convoluted way to demonstrate assignment operators in Java
import java.util.Scanner;
public class AssignmentOperators {
public static void main(String args[]) {
int k = 5;
double d;
Scanner myScanner = new Scanner(System.in);
System.out.println("select the assignment operator you want to use: \n" + "1. addition\n" + "2. substraction\n" + "3. multiplication \n" + "4. division \n");
@PatchRowcester
PatchRowcester / FindWithinHorizon_Test.java
Created January 4, 2013 02:38
Accept a character and display it. Also, accept a string and look for two consecutive digits and display them.
import java.util.Scanner;
public class FindWithinHorizon_Test {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
char t,x;
@PatchRowcester
PatchRowcester / int_to_binary.py
Last active December 14, 2015 04:09
This is python code to convert an integer to its binary form.
x = int(raw_input('enter an int: '))
isNeg = False
if x < 0:
x = abs(x)
isNeg = True
result =''
if x == 0:
result = '0'
while x>0:
result = str(x%2) + result
@PatchRowcester
PatchRowcester / nearest_whole_number.py
Created February 27, 2013 01:27
A simple python program to accept a float and print its nearest whole number
x = float(raw_input('Enter a decimal number: '))
a = int(x)
c = (float(a + (a+1))/2)
#print ('c = ' + str(c))
if x >= c:
b = int(x) + 1
@PatchRowcester
PatchRowcester / while_number_check.py
Created February 27, 2013 02:40
Simplest way to check if an entered number is a whole number or not.
x = float(raw_input('Enter any number (decimal or integer)'))
if (x%1 ==0):
print('Whole number')
else:
print('Not a whole number')
@PatchRowcester
PatchRowcester / decimal_fraction_to_binary.py
Created February 27, 2013 03:34
This is a way to convert a number between 0 & 1 to its binary form in Python
x = float(raw_input('Enter a number between 0 and 1: '))
p = 0
#if a number is a whole number, the reminder when divided by 1 is always 0
#if a number is not a whole number, the reminder when divided by 1 is NOT 0
#Because we are trying to convert a decimal to a whole number, this check is necessary
while (((2**p) * x)%1 !=0):
print('Remainder = ' + str((2**p)*x - int((2**p)*x)))
p += 1
num = int(x*(2**p))
print str(p)
@PatchRowcester
PatchRowcester / any_number_to_binary.py
Created February 28, 2013 04:00
This python program will convert any given number to its binary form
#accept a number, and return its binary form
x = float(raw_input('Enter a number: '))
isNeg = False
if x < 0:
x = abs(x)
isNeg = True
resultInt = ''
resultFlt = ''
@PatchRowcester
PatchRowcester / clip.py
Created March 20, 2013 02:17
Takes in three numbers and returns a value based on the value of x. Returns: - lo, when x < lo - hi, when x > hi - x, otherwise
def clip(lo, x, hi):
'''
Takes in three numbers and returns a value based on the value of x.
Returns:
- lo, when x < lo
- hi, when x > hi
- x, otherwise
'''
# Your code here
return min(max(x,lo),hi)
@PatchRowcester
PatchRowcester / odd.py
Created March 26, 2013 03:40
A program to accept a number, and return True if it is odd, and False if it is even without using IF keyword
def odd(x):
'''
x: int or float.
returns: True if x is odd, False otherwise
'''
# Your code here
y = x%2
return bool(y)
@PatchRowcester
PatchRowcester / isVowel.py
Created March 26, 2013 03:51
Program to that will accept a string and return True if the string is a single character vowel of any case. False if not.
def isVowel(char):
'''
char: a single letter of any case
returns: True if char is a vowel and False otherwise.
'''
# Your code here
if ((char =='a') or (char=='e') or (char == 'i') or (char == 'o') or (char =='u')):
return True
elif ((char =='A') or (char=='E') or (char == 'I') or (char == 'O') or (char =='U')):