-
-
Notifications
You must be signed in to change notification settings - Fork 92
Glasgow | 25-SDC-NOV | Katarzyna Kazimierczuk | Sprint 4 | Python shell tools #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
9f69078
abb6c89
15f4549
cb9b93b
2ee678b
d32f791
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| def cleanInput(listOfFiles): | ||
| cleanLinesArr = [] | ||
|
|
||
| for file in listOfFiles: | ||
| grabbedText = Path(file).read_text(encoding="utf-8") | ||
| splitLines = grabbedText.split("\n") | ||
| cleanLinesArr.extend(splitLines) | ||
|
|
||
| return cleanLinesArr | ||
|
|
||
| args = sys.argv[1:] | ||
| flag = None | ||
| restIsFiles = [] | ||
|
|
||
| if len(args) > 0 and args[0] and args[0][0] == "-": | ||
| flag = args[0] | ||
| restIsFiles = args[1:] | ||
| else: | ||
| flag = None | ||
| restIsFiles = args | ||
|
|
||
| def takeSpecifiedAction(cleanLinesArr, flag): | ||
|
katarzynakaz marked this conversation as resolved.
|
||
| countingOnlyFullLines = 1 | ||
|
|
||
| for file in cleanLinesArr: | ||
|
katarzynakaz marked this conversation as resolved.
Outdated
|
||
| if not flag: | ||
| print(file) | ||
| elif flag == "-n": | ||
| print(f"{countingOnlyFullLines} {file}") | ||
| countingOnlyFullLines += 1 | ||
| elif flag == "-b": | ||
| if file == "": | ||
| print("") | ||
| else: | ||
| print(f"{countingOnlyFullLines} {file}") | ||
| countingOnlyFullLines += 1 | ||
| else: | ||
| print("incorrect flag") | ||
|
katarzynakaz marked this conversation as resolved.
Outdated
|
||
|
|
||
| lines = cleanInput(restIsFiles) | ||
| takeSpecifiedAction(lines, flag) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import sys | ||
| import os | ||
|
|
||
| # `ls -1` | ||
| def showAllFilesInDir(directory): | ||
| listOfFiles = os.listdir(directory) | ||
|
|
||
| for eachFile in listOfFiles: | ||
| if eachFile[0] != ".": | ||
| print(eachFile) | ||
|
|
||
| # `ls -1 sample-files` | ||
| def showVisibleInSampleFiles(): | ||
| listOfFiles = os.listdir("sample-files") | ||
|
katarzynakaz marked this conversation as resolved.
Outdated
|
||
|
|
||
| for eachFile in listOfFiles: | ||
| if eachFile[0] != ".": | ||
| print(eachFile) | ||
|
|
||
| # `ls -1 -a sample-files` | ||
| def showAllInSampleFiles(): | ||
| listOfFiles = os.listdir("sample-files") | ||
|
|
||
| for eachFile in listOfFiles: | ||
| print(eachFile) | ||
|
|
||
| argv = sys.argv[1:] | ||
|
|
||
| if "-a" in argv: | ||
| showAllInSampleFiles() | ||
| elif "sample-files" in argv: | ||
|
katarzynakaz marked this conversation as resolved.
Outdated
|
||
| showVisibleInSampleFiles() | ||
| else: | ||
| showAllFilesInDir(".") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| def calculateCounts(inputFiles): | ||
| return { | ||
|
katarzynakaz marked this conversation as resolved.
Outdated
|
||
| "lines": len(inputFiles.split("\n")) - 1, | ||
|
katarzynakaz marked this conversation as resolved.
Outdated
|
||
| "words": len(inputFiles.split()), | ||
| "bytes": len(inputFiles), | ||
| } | ||
|
|
||
| # * `wc -l sample-files/3.txt` | ||
| # * `wc -l sample-files/*` | ||
| def countLines(listOfFiles): | ||
| for file in listOfFiles: | ||
| content = Path(file).read_text(encoding="utf-8") | ||
|
|
||
| counts = calculateCounts(content) | ||
| print(f"{counts['lines']} {file}") | ||
|
|
||
| # * `wc -w sample-files/3.txt` | ||
| def countWords(listOfFiles): | ||
| for file in listOfFiles: | ||
| content = Path(file).read_text(encoding="utf-8") | ||
|
|
||
| # const wordsCounted = content.split(" ").filter(word => word !== "").length; | ||
| counts = calculateCounts(content) | ||
| print(f"{counts['words']} {file}") | ||
|
|
||
| # * `wc -c sample-files/3.txt` | ||
| def countBytes(listOfFiles): | ||
|
katarzynakaz marked this conversation as resolved.
Outdated
|
||
| for file in listOfFiles: | ||
| content = Path(file).read_text(encoding="utf-8") | ||
| counts = calculateCounts(content) | ||
| print(f"{counts['bytes']} {file}") | ||
|
|
||
| # * `wc sample-files/*` | ||
| def countAll(listOfFiles): | ||
| for file in listOfFiles: | ||
| content = Path(file).read_text(encoding="utf-8") | ||
| counts = calculateCounts(content) | ||
| print(f"{counts['lines']} {counts['words']} {counts['bytes']} {file}") | ||
|
|
||
| argv = sys.argv[1:] | ||
| files = [arg for arg in argv if not arg.startswith("-")] | ||
|
|
||
| if "-l" in argv: | ||
| countLines(files) | ||
| elif "-w" in argv: | ||
| countWords(files) | ||
| elif "-c" in argv: | ||
| countBytes(files) | ||
| else: | ||
| countAll(files) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,61 +5,61 @@ Do not convert any binary numbers to decimal when solving a question unless the | |
| The goal of these exercises is for you to gain an intuition for binary numbers. Using tools to solve the problems defeats the point. | ||
|
|
||
| Convert the decimal number 14 to binary. | ||
| Answer: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is not part of this sprint. You need to do a git restore of the file to get it back to the original state
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Github doesn't agree. It is showing differences. If you do a restore to the start point of thie PR then this file shouldn't be showing up in the PR list at all.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you, that's now done |
||
| Answer:1110 | ||
|
katarzynakaz marked this conversation as resolved.
Outdated
|
||
|
|
||
| Convert the binary number 101101 to decimal: | ||
| Answer: | ||
| Answer:45 | ||
|
|
||
| Which is larger: 1000 or 0111? | ||
| Answer: | ||
| Answer:1000 | ||
|
|
||
| Which is larger: 00100 or 01011? | ||
| Answer: | ||
| Answer:01011 | ||
|
|
||
| What is 10101 + 01010? | ||
| Answer: | ||
| Answer:11111 | ||
|
|
||
| What is 10001 + 10001? | ||
| Answer: | ||
| Answer:100010 | ||
|
|
||
| What's the largest number you can store with 4 bits, if you want to be able to represent the number 0? | ||
| Answer: | ||
| Answer:1111 so 15 | ||
|
|
||
| How many bits would you need in order to store the numbers between 0 and 255 inclusive? | ||
| Answer: | ||
| Answer:8 as 2 **8 = 256 0-255 | ||
|
|
||
| How many bits would you need in order to store the numbers between 0 and 3 inclusive? | ||
| Answer: | ||
| Answer: 2 **2 0 1 2 3 | ||
|
|
||
| How many bits would you need in order to store the numbers between 0 and 1000 inclusive? | ||
| Answer: | ||
| Answer:2** 10 = 1024 so 10 | ||
|
|
||
| How can you test if a binary number is a power of two (e.g. 1, 2, 4, 8, 16, ...)? | ||
| Answer: | ||
| Answer: number /2 until it reaches 1, check remainder at each division | ||
|
|
||
| Convert the decimal number 14 to hex. | ||
| Answer: | ||
| Answer: E | ||
|
|
||
| Convert the decimal number 386 to hex. | ||
| Answer: | ||
| Answer:0x182 863/16 =24 %2 24/16=1 %8 | ||
|
|
||
| Convert the hex number 386 to decimal. | ||
| Answer: | ||
| Answer:902 | ||
|
|
||
| Convert the hex number B to decimal. | ||
| Answer: | ||
| Answer:11 | ||
|
|
||
| If reading the byte 0x21 as a number, what decimal number would it mean? | ||
| Answer: | ||
| Answer: 33 | ||
|
|
||
| If reading the byte 0x21 as an ASCII character, what character would it mean? | ||
| Answer: | ||
| Answer:! | ||
|
|
||
| If reading the byte 0x21 as a greyscale colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? | ||
| Answer: | ||
| Answer:33 intensity so grey dark | ||
|
|
||
| If reading the bytes 0xAA00FF as an RGB colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? | ||
| Answer: | ||
| Answer:RGB(170, 0, 255) | ||
|
|
||
| If reading the bytes 0xAA00FF as a sequence of three one-byte decimal numbers, what decimal numbers would they be? | ||
| Answer: | ||
| Answer: 170, 0, 255 | ||
Uh oh!
There was an error while loading. Please reload this page.