Giter Site home page Giter Site logo

summer-work's Introduction

summer-work

These are coding projects done during the summer of 2023. This also includes coding files for the edx AP CSA course.

In progress: baseConverter (java and python), Uno score tracker app

summer-work's People

Contributors

oopdawg avatar zeugma4242 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

summer-work's Issues

HashMap are always the same whatever the BaseConverter instance we use. Turn them static

// adds digits 0 to 9
for (int i = 0; i < 10; i++){
digitToValue.put(Integer.toString(i), i);
valueToDigit.put(i, Integer.toString(i));
}
// adds letters A to Z
int value = 0;
for (char c = 'A'; c <= 'Z'; c++){ // c cycles through all the letters with ASCII values
value = 10 + (c - 'A'); // Calculates value using ASCII values. The +10 is because 0 through 9 is already mapped to the digits
digitToValue.put(Character.toString(c), value);
valueToDigit.put(value, Character.toString(c));
}
}
// Dictionary to get character digits to int value
// eg A = 10
HashMap<String, Integer> digitToValue = new HashMap<>();
// Dictionary to get int value to digits
HashMap<Integer, String> valueToDigit = new HashMap<>();

The initialization code in the constructor should be then moved to a static initializer block

Avoid copy paste of code outside of while loop

endDigits = []
quotient = numBase10//endBase
remainder = numBase10%endBase
endDigits.append(remainder)
while quotient != 0:
remainder = quotient%endBase
quotient //= endBase
endDigits.append(remainder)

With a bit of logic, no need to copy code and run it once out of the loop.
Goal is to run the loop at list once, whatever the quotient value to begin with.
This is true is the endDigits is empty, so we can test for that:

endDigits = []
quotient = numBase10

while quotient or not endDigits:
    quotient, remainder = divmod(quotient, endBase)
    endDigits.append(remainder)

Alternative writing

digits = [c for c in num]
digits.reverse()

reverse is fine to reverse the sequence. however, as the list was just built, an alternative is to create the list out of the sequence already in the right order:

num[::-1] # returns the sequence in reverse. num = '123' will return '321'

list(sequence) create a list which each element from the sequence. An str is a sequence and each element is a char.

so list(num[::-1]) give the same result.

To be a bit more readable, 'reversed' is a built-in function reversing the sequence order

list(reversed('123')) works too

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.