Lab 2 - Islamic University of Gaza
Transcript of Lab 2 - Islamic University of Gaza
The Islamic University of Gaza ECOM 3011: Discrete Mathematic
Faculty of Engineering Instructor: Pro. Mohammed Al Hanjouri
Department of Computer Engineering T.A.: Eng. Ahmed M Bader El-Din
Lab 2
Eng. Ahmed M Bader El-Din November 24, 2018
Part 1 : Operations OF sets
Part 1: Write code to receive two sets and print the sets, union, intersection, Cartesian product, power of the 1st one, and the difference (1st – 2nd)?
1- Union :
2- Intersection :
3- Difference :
------------------------------------------- Output ---------------------------------------------
A: [0, 1, 2, 3] B: [2, 3, 4, 5] Union of two Set [0, 1, 2, 3] and [2, 3, 4, 5] in Java is [0, 1, 2, 3, 4, 5] Intersection of two Set [0, 1, 2, 3] and [2, 3, 4, 5] in Java is [2, 3] Difference of two Set [0, 1, 2, 3] and [2, 3, 4, 5] in Java is [0, 1] Cartesian Product between two Set [[0, 2], [0, 3], [0, 4], [0, 5], [1, 2], [1, 3], [1, 4], [1, 5], [2], [2, 3], [2, 4], [2, 5], [2, 3], [3], [3, 4], [3, 5]]
4- Set Power
set P(S) of a set S is the set of all subsets of S
E.g. S = {a, b, c} then P(s) = {{}, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}. If S has n
elements in it then P(s) will have 2^n elements.
Algorithm:
----------------------------------------- Input ------------------------------------------
1- Set [], set_size
---------------------------------------- Procedure -------------------------------------
1. Get the size of power set
powet_set_size = POW (2, set_size)
2. Loop for counter from 0 to pow_set_size
(a) Loop for i = 0 to set_size
(i) If ith bit in counter is set
Print ith element from set for this subset
(b) Print separator for subsets i.e., newline
---------------------------------------- Example --------------------------------------
Set = [a, b, c]
power_set_size = POW (2, 3) = 8
Run for binary counter = 000 to 111
Value of Counter Subset
000 -> Empty set
001 -> a
010 -> b
011 -> ab
100 -> c
101 -> ac
110 -> bc
111 -> abc
------------------------------------------ Code ------------------------------------------
------------------------------------------- Output ---------------------------------------------
a b ab c ac bc abc BUILD SUCCESSFUL (total time: 0 seconds)
Q1) Can you now get this output?
Power Set
[[], [a], [b], [a, b], [c], [a, c], [b, c], [a, b, c]]
------------------------------------------- Output ---------------------------------------------
run: First set : [A, B, C] Second set : [B, D, F] New List : [A, B, C, D, F] BUILD SUCCESSFUL (total time: 1 second)
HW#2:
The Caesar cipher shifts all the letters in a piece of text by a certain number of
places. The key for this cipher is a letter, which represents the number of
place for the shift. Therefore, for example, a key D means “shift 3 places" and
a key M means, “shift 12 places". Note that a key A means “do not shift" and a
key Z can either mean “shift 25 places" or “shift one place backwards". For
example, the word “CAESAR” with a shift P becomes “RPTHPG”.
- Write Algorithm for Caesar Cipher (Input, Procedure) and give example for
your algorithm.
- Write code for Caesar's cipher.