-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
277 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from visma.simplify.simplify import simplify | ||
from visma.functions.constant import Constant | ||
from visma.io.tokenize import tokenizer | ||
|
||
|
||
def logicalAND(token1, token2): | ||
# TODO: Comments, animations & test cases. | ||
comments = [] | ||
animations = [] | ||
token1, _, _, _, _ = simplify(token1) | ||
token2, _, _, _, _ = simplify(token2) | ||
if isinstance(token1, Constant) and isinstance(token2, Constant): | ||
comments += [['Converting numbers to Binary Illustrations: ']] | ||
animations += [[]] | ||
binaryValue1 = token1.binary() | ||
binaryValue2 = token2.binary() | ||
comments += [[]] | ||
animations += [[tokenizer('a = ' + str(binaryValue1))]] | ||
comments += [[]] | ||
animations += [[tokenizer('b = ' + str(binaryValue2))]] | ||
comments += [['Doing AND operation for each of the consecutive bit']] | ||
animations += [[]] | ||
resultValue = token1.calculate() & token2.calculate() | ||
comments += [['Final result is']] | ||
animations += [[tokenizer('r = ' + str(resultValue))]] | ||
token_string = 'r = ' + str(resultValue) | ||
return token_string, animations, comments | ||
else: | ||
return '', [], [] | ||
|
||
|
||
def logicalOR(token1, token2): | ||
# TODO: Comments, animations & test cases. | ||
comments = [] | ||
animations = [] | ||
token1, _, _, _, _ = simplify(token1) | ||
token2, _, _, _, _ = simplify(token2) | ||
if isinstance(token1, Constant) and isinstance(token2, Constant): | ||
comments += [['Converting numbers to Binary Illustrations: ']] | ||
animations += [[]] | ||
binaryValue1 = token1.binary() | ||
binaryValue2 = token2.binary() | ||
comments += [[]] | ||
animations += [[tokenizer('a = ' + str(binaryValue1))]] | ||
comments += [[]] | ||
animations += [[tokenizer('b = ' + str(binaryValue2))]] | ||
comments += [['Doing OR operation for each of the consecutive bit']] | ||
animations += [[]] | ||
resultValue = token1.calculate() | token2.calculate() | ||
comments += [['Final result is']] | ||
animations += [[tokenizer('r = ' + str(resultValue))]] | ||
token_string = 'r = ' + str(resultValue) | ||
return token_string, animations, comments | ||
else: | ||
return '', [], [] | ||
|
||
|
||
def logicalNOT(token1): | ||
# TODO: Test cases. | ||
comments = [] | ||
animations = [] | ||
token1, _, _, _, _ = simplify(token1) | ||
if isinstance(token1, Constant): | ||
comments += [['Converting numbers to Binary Illustrations: ']] | ||
animations += [[]] | ||
binaryValue1 = token1.binary() | ||
comments += [[]] | ||
animations += [[tokenizer('a = ' + str(binaryValue1))]] | ||
resultValueBinary = bin((1 << 8) - 1 - int(binaryValue1, 2)) | ||
resultValue = int(resultValueBinary, 2) | ||
comments += [['Final binary is']] | ||
animations += [[tokenizer('r = ' + str(resultValueBinary))]] | ||
comments += [['Final result is']] | ||
animations += [[tokenizer('r = ' + str(resultValue))]] | ||
token_string = 'r = ' + str(resultValue) | ||
return token_string, animations, comments | ||
else: | ||
return '', [], [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from visma.io.tokenize import tokenizer | ||
|
||
|
||
def simpleProbability(sampleSpace, requiredEvent=None): | ||
animations = [] | ||
comments = [] | ||
events = [] | ||
token_string = '' | ||
if sampleSpace.values is not []: | ||
events.extend(sampleSpace.values) | ||
totalOccurances = len(events) | ||
animations += [[]] | ||
comments += [['The total occurances are ' + str(totalOccurances)]] | ||
requiredOccurances = events.count(requiredEvent) | ||
animations += [[]] | ||
comments += [['The occurances of required event are ' + str(requiredOccurances)]] | ||
probability = requiredOccurances/totalOccurances | ||
comments += [['Hence, Required probability is: ']] | ||
animations += [tokenizer('P = ' + str(probability))] | ||
token_string = 'P = ' + str(probability) | ||
return token_string, animations, comments | ||
else: | ||
return '', [], [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from collections import Counter | ||
from visma.io.tokenize import tokenizer | ||
from visma.simplify.simplify import simplify | ||
from visma.io.parser import tokensToString | ||
from visma.functions.constant import Constant | ||
|
||
|
||
class sampleSpace(object): | ||
values = [] | ||
size = 0 | ||
|
||
def __init__(self, values): | ||
if values is not None: | ||
self.values = values | ||
self.size = len(values) | ||
|
||
|
||
def ArithemeticMean(sampleSpace): | ||
animations = [] | ||
comments = [] | ||
if sampleSpace.values is not []: | ||
sum = sum(sampleSpace.values) | ||
animations += [[]] | ||
comments += [['Sum of all the values of the sample space provided by user: ' + sum]] | ||
summationString = '' | ||
for val in sampleSpace.values: | ||
summationString += str(val) + '+' | ||
summationString = summationString[:-1] | ||
summationTokens = tokenizer(summationString) | ||
resultTokens, _, _, _, _ = simplify(summationTokens) | ||
if len(resultTokens) == 1 and isinstance(resultTokens, Constant): | ||
ArithemeticMean = resultTokens/Constant(len(sampleSpace.values)) | ||
animations += [[]] | ||
comments += [['Considering ' + len(sampleSpace.values) + ' values.']] | ||
animations += [[tokenizer('mean = ' + str(ArithemeticMean.calculate))]] | ||
token_string = tokensToString(ArithemeticMean) | ||
return token_string, animations, comments | ||
else: | ||
return '', [], [] | ||
|
||
|
||
def Mode(sampleSpace): | ||
animations = [] | ||
comments = [] | ||
token_string = '' | ||
if sampleSpace.values is not []: | ||
mode, frequency = Counter(sampleSpace.values).most_common(1)[0] | ||
comments += [['The mode refers to the most occuring element']] | ||
animations += [[]] | ||
comments += [['Mode = ' + str(mode) + '; Mode Frequence = ' + str(frequency)]] | ||
animations += [[]] | ||
token_string = 'Mode = ' + str(mode) + '; Mode Frequence = ' + str(frequency) | ||
return token_string, animations, comments | ||
else: | ||
return '', [], [] | ||
|
||
|
||
def Median(sampleSpace): | ||
animations = [] | ||
comments = [] | ||
token_string = '' | ||
if sampleSpace.values is not []: | ||
sizeSampleSpace = sampleSpace.size | ||
if sizeSampleSpace % 2 == 1: | ||
medianValue = sorted(sampleSpace.values)[sizeSampleSpace//2] | ||
else: | ||
medianValue = sum(sorted(sampleSpace.values)[sizeSampleSpace//(2-1): sizeSampleSpace//2+1])/2.0 | ||
comments += [['The median refers to the middle element in sorted sample space']] | ||
animations += [[]] | ||
comments += [['Median = ' + str(medianValue)]] | ||
animations += [[]] | ||
token_string = 'Median = ' + str(medianValue) | ||
return token_string, animations, comments | ||
else: | ||
return '', [], [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.