Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
"""CSC108 Assignment 2 functions""" from typing import List # Used to determine whether to encrypt or decrypt ENCRYPT = 'e' DECRYPT = 'd' def clean_message(message: str) -> str: """Return a string with only uppercase letters from message with non- alphabetic characters removed. >>> clean_message('Hello world!') 'HELLOWORLD' >>> clean_message("Python? It's my favourite language.") 'PYTHONITSMYFAVOURITELANGUAGE' >>> clean_message('88test') 'TEST' """ # WRITE THE BODY OF clean_message HERE clean_string = "" for c in message: if c.isalpha(): clean_string = clean_string + c.upper() return clean_string # WRITE THE REST OF YOUR A2 FUNCTIONS HERE def encrypt_letter(letter: str, value: int) -> str: """ Applies keystream value to the letter to encrpyt the letter, and returns the result. Parameters: letter (str): Single uppercase letter to encrypt value (int): Keystream value Returns: str: The encrypted letter """ return None def decrypt_letter(letter: str, value: int) -> str: """ Applies keystream value to the letter to decrpyt the letter, and returns the result. Parameters: letter (str): Single uppercase letter to decrypt value (int): Keystream value Returns: str: The decrypted letter """ return None def is_valid_deck(deck: List[int]) -> bool: """ Checks if a deck is valid (contains every integer from 1 to the number of cards in the deck) Parameters: deck (List[int]): A candidate deck of cards Returns: bool: True if the candidate deck is a valid deck of cards """ for i in range(1, len(deck) + 1): if deck.count(i) != 1: return False return True def swap_cards(deck: List[int], index: int) -> None: """ Swap the card at an idex with the card after it, treat deck as circular connected Parameters: deck (List[int]): A candidate deck of cards index (int): The index at which to swap with following card """ swap_index = index + 1 if index == len(deck) - 1: swap_index = 0 temp = deck[swap_index] deck[swap_index] = deck[index] deck[index] = temp return None def get_small_joker_value(deck: List[int]) -> int: """ Gets the value of the smaller joker card Parameters: deck (List[int]): A candidate deck of cards Returns: int: The value of the small joker """ max = 0 for i in deck: if i > max: max = i return max - 1 def get_big_joker_value(deck: List[int]) -> int: """ Gets the value of the big joker card Parameters: deck (List[int]): A candidate deck of cards Returns: int: The value of the larger joker """ max = 1 for i in deck: if i > max: max = i return max def move_small_joker(deck: List[int]) -> None: """ Swap the small joker with the following card, treat as circular connected Parameters: deck (List[int]): A candidate deck of cards """ swap_cards(deck, deck.index(get_small_joker_value(deck))) return None def move_big_joker(deck: List[int]) -> None: """ Swap the big joker with the following card, treat as circular connected Parameters: deck (List[int]): A candidate deck of cards """ swap_cards(deck, deck.index(get_big_joker_value(deck))) swap_cards(deck, deck.index(get_big_joker_value(deck))) return None def triple_cut(deck: List[int]) -> None: """ Do a triple cut on the deck Parameters: deck (List[int]): A candidate deck of cards """ s_j_index = deck.index(get_small_joker_value(deck)) b_j_index = deck.index(get_big_joker_value(deck)) f_j_index = s_j_index if s_j_index < b_j_index else b_j_index sd_j_index = s_j_index if s_j_index > b_j_index else b_j_index if sd_j_index == len(deck) - 1: deck[:] = deck[f_j_index:] + deck[:f_j_index] elif f_j_index == 0: deck[:] = deck[sd_j_index + 1:] + deck[:sd_j_index + 1] else: deck[:] = deck[sd_j_index+1:] + \ deck[f_j_index:sd_j_index + 1] + deck[:f_j_index] return None def insert_top_to_bottom(deck: List[int]) -> None: """ Examine the value of the bottom card and move that many cards from the top of the deck to the bottom, just above the bottom card (If bottom card is big joker, use small joker value as number of cards) Parameters: deck (List[int]): A candidate deck of cards """ bottom_index = deck[len(deck) - 1] if bottom_index == get_big_joker_value(deck): bottom_index -= 1 deck[:] = deck[bottom_index:len(deck)-1] + \ deck[:bottom_index]+deck[len(deck) - 1: len(deck)] return None def get_card_at_top_index(deck: List[int]) -> int: """ Using the top value of the card as an index, return the card in the deck at that index (If top card is big joker, use small joker value as index) Parameters: deck (List[int]): A candidate deck of cards Returns: int: The value of the top index card """ index = deck[0] if get_big_joker_value(deck) == index: index = get_small_joker_value(deck) return deck[index] def get_next_keystream_value(deck: List[int]) -> int: """ Repeats algorithm steps until a valid keystream value is produced Parameters: deck (List[int]): A candidate deck of cards Returns: int: Return the valid keystream value """ keystream_value = -1 while keystream_value < 1 or (keystream_value == get_small_joker_value(deck) or keystream_value == get_big_joker_value(deck)): move_small_joker(deck) move_big_joker(deck) triple_cut(deck) insert_top_to_bottom(deck) keystream_value = get_card_at_top_index(deck) return keystream_value def process_messages(deck: List[int], messages: List[str], type: str) -> List[str]: """ Process the encrypted or decrypted messages Parameters: deck (List[int]): A candidate deck of cards message (List[str]): List of unprocessed encrypted or decrypted messages type (str): ENCRYPT or DECRYPT Returns: List[str]: List of processed encrypted or decrypted messages """ result = [] if type == ENCRYPT: for i in range(0, len(messages)): message_values = [] key_values = [] messages[i] = clean_message(messages[i]) s = "" for j in range(0, len(messages[i])): message_values.append(ord(messages[i][j]) - 64) key_values.append(get_next_keystream_value(deck)) char_val = ((message_values[j] + key_values[j]) % (len(deck) - 2)) + 64 if char_val == 64: char_val = len(deck) - 2 + 64 print(char_val-64) s += chr(char_val) result.append(s) if type == DECRYPT: for i in range(0, len(messages)): message_values = [] key_values = [] messages[i] = clean_message(messages[i]) s = "" for j in range(0, len(messages[i])): message_values.append(ord(messages[i][j]) - 64) key_values.append(get_next_keystream_value(deck)) char_val = ((message_values[j] - key_values[j]) % (len(deck) - 2)) + 64 if char_val == 64: char_val = len(deck) - 2 + 64 s += chr(char_val) result.append(s) return result # This if statement should always be the last thing in the file, below all of # your functions: if __name__ == '__main__': """Did you know that you can get Python to automatically run and check your docstring examples? These examples are called "doctests". To make this happen, just run this file! The two lines below do all the work. For each doctest, Python does the function call and then compares the output to your expected result. NOTE: your docstrings MUST be properly formatted for this to work! In particular, you need a space after each >>>. Otherwise Python won't be able to detect the example. """ import doctest doctest.testmod()
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
Untitled
13 hours ago | 0.16 KB
settings
13 hours ago | 0.10 KB
IT & AI
22 hours ago | 1.62 KB
Stationeers - Sign Tags from Power Distributi...
HTML | 1 day ago | 2.00 KB
PM: Shopify Client Edits
1 day ago | 0.19 KB
PM: Shopify Assigning Design Task 2
1 day ago | 0.14 KB
PM: Shopify Assigning Design Task 1
1 day ago | 0.32 KB
Commodore Callback 8020
1 day ago | 0.18 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!