#Programming and CS Questions

1 messages · Page 1 of 1 (latest)

crisp zinc
#

know any good guides for the swing library?

crisp zinc
#

Thank

vital jolt
#

Haven't seen Java in a while but isn't JavaFX more easier?

toxic bear
#

hello, im learning recursion in python and im getting stuck on understanding how to retain state (?) across recursive calls to the function from inside itself

#

where does a returned output "go"??

#
def factorial(n):
    the_fact = 1
    if n <= 1:
        return the_fact
    else:
        the_fact = n * factorial(n-1)
        n = n - 1
        return the_fact

(the code works but i dont fully know why)

#

this is a factorial maker i wrote

#

but i thought return ends everything and gives you the output

#

so how is it working

#

but isn't it calling the_fact = 1 every time i recursively call the function, meaning it resets the value to 1?

#

right yeah

#

thank you, this part makes sense now

#

so each recursive call is like quarantined inside (?) and the "return" basically turns the recursive call into its output insofar as the rest of the code is concerned?

#

is it correct to say that ```py
factorial(5)
= 5 * factorial(4)
= 5 * 4 * factorial(3)
= 5 * 4 * 3 * factorial(2)
= 5 * 4 * 3 * 2 * factorial(1)

#

well i guess they are not equal since it only goes one way

#

but you can still write = for normal maths functions

#

wtf is a return

#

and does it make sense to think of five() as being equal to 5 in the same way that x is equal to 5 (is it like a variable assignment)?

#

hmmm no i think im completely misunderstanding it

#

yeah exactly!!

#

thank you, this is really helping me understand it

#

right yeha i tried running it and it printed the return parts separately

#

(i added str(n) to the prints)

toxic bear
#

hello I have a question

#

what is the point of making variables private (in C#)?

#

why would I want to hide variables from other classes

solar abyss
# toxic bear what is the point of making variables private (in C#)?

it doesnt have effect on how the code functions but it's a useful practice for organizing larger programs, especially when collaborating. basically it's part of a broader set of practices to make code more modular.

for instance let's say you have a Patient class with a bodyTempC and bodyTempF variable. naturally you want these variables to be linked; you shouldn't be able to change bodyTempC without also changing bodyTempF.

in this case it would be useful to have a setBodyTemp() function that updates both C and F at the same time. however, you might forget about this function, and instead run bodyTempC = 41 manually, which would break bodyTempF. as such, it's useful to set both bodyTempC and bodyTempF to private, so you don't accidentally touch them directly.

#

haha i got ninjad by irk

toxic bear
#

I think I get it

#

it's to restrict points of interface for classes so that you are more restricted in how you interact with the contents of a class

#

kind of like a cell wall

solar abyss
#

hahaha

#

now you have to prove that youre human

#

@toxic bear different languages enforce private variables differently. C# won't compile if you try to access a private member. on the other hand, python doesn't even have a private keyword. it's accepted practice that you put a _ in front of private variables like _body_temp_C, and modern IDEs will get angry at you for accessing a private member, but the language itself doesn't enforce anything. a lot of people hate this but i kind of like it because its more flexible

#

also private member is funny

toxic bear
dry breach
#

@solid notch what languages do you think I should focus on, as a student still

solar abyss
#

o rly? i find that python’s typing and basic data structure syntax are a lot easier to whip out in an interview

#

but im a python fan

#

i dont think big python codebases are any more of a nightmare than other languages

#

typed python rules tho

solar abyss
#

tru tho a good linter + typed python takes care of those issues i think

toxic bear
#

hey is there a way to bulk name variables in python

#

idk if you can see what im doing; basically ive got another class that im making instances from, and i want to make a bunch of them and add them to a dict inside of an instance of this other class

#

i thought itd be convenient to iteratively name them like sack_1, sack_2 etc and add them into the dict one by one but i cant figure out how to do that

#

if i did it manually id have to do

#

and so on

#

i guess i can just use a dictionary since im gonna put them all into a dictionary anyway

crisp zinc
#

I think that's basically object oriented programming which python doesn't do I thought

#

Oh I was wrong python does do object oriented programming we just didn't get to it in the class I took I think

toxic bear
#

whats the best way to show my code to ppl for feedback

crisp zinc
#

Maybe github?

toxic bear
#

but isnt that for like completed code

crisp zinc
#

Guess it depends how complicated the code you want to share is. I've heard Jupyter notebooks are neat for sharing code as well i think? Also I think it might make sense to use an array and then pull data from it but depends what ur trying to do.

#

But I didn't get very far in coding so take my input with a grain of salt

magic pumice
#

as you say you can also use a dictionary

#

which one is better depends on your use case

#

there's no easy way to use a format string to access a variable name without a dictionary / an object w string keys

#

but also from what im seeing you could just do self.add_sack(Knapsack(self.trove)), no need to name the variable as it'll get disposed of anyway

#

like is there a reason you don't want to do this?

def generate_sacks(self):
    for i in range(self.pool_size):
         sack = Knapsack(self.trove)
         self.add_sack(sack)
#

i.e. is there a reason you need them to be iteratively named and retrieved, instead of just created and added to the sack?

toxic bear
magic pumice
#

solving knapsack with a genetic algorithm cool

toxic bear
#

hey guys

#

ive made a simplified version and it's almost working

#

but it's throwing an index out of range error for my cross() function and i cant figure out why

#

normally i use pythontutor to visualise the code if im stuck but it's too big to feed into it

#

do you know a good alternative i can use to debug it

toxic bear
#

oh i figured it out

#

i was using the elements within a list as the index value instead of the index value of the element

#

bruh look at dis dood... the breeding process is broken and some of the offspring are coming out super short

toxic bear
#

ive fixed the evolution and it evolves indefinitely now

#

now im working on making it detect when it's reached a solution

#

also the mating and mutation are too random

#

so progress isn't consistent

#

ive got the assortative mating working (it always crosses the top 2 sacks)

#

it gets homogenous really fast

#

so fascinating goddamn...

#

i wonder why it sometimes regresses across generations

#

it seems like it shouldn't lose the highest value sacks between generations

#

i guess it's just the mutation knocking out some genes from the top performers (bad luck)

#

maybe mutation should only hjappen as a result of mating??

magic pumice
#

not sure where the state of your code is at but it might be worth writing rigorous but simple sanity tests for the most important/nontrivial aspects of your code

#

unless you're already certain of the correctness of each part

#

it's just good to verify that every piece is working right before drawing conclusions about the benefit of one strategy over another

toxic bear
#

@magic pumice @solid notch can you recommend me a resource to start learning how to do tests? Is this the best one: https://docs.python.org/3/library/unittest.html

dry breach
#

This is a broad question, but do you guys have any advice for before I dive into the deep end of upper division CS? I'm not quite sure what to expect and it feels a bit overwhelming. I have a creeping feeling I'm just not going to remember all the skills from earlier classes I will need.

crisp zinc
#

do you have the files and notes from earlier classes

#

try to keep them organized so you can refer back to them if you need to

#

and put on dropbox or google docs or something maybe

dry breach
#

I do, I could organize them better I think. I also still have lots of ebooks.

simple mountain
#

hello

I am in a pickle,

I'm trying to make a project where using a google sheet data I can output an interface(google form or google docs accessible via QR code) where it shows to a customer the number of reward points/stars they have.

The goal is just to give a card to a customer with just a QR code, then if they want to see their "profile" (i,e how many reward points they have, they can just scan it and it will show a document/form thing with their name and total points)

Now, I have discovered this Google appscript thing, that seems to be able to do this
But I dont know how it works, the first thing I've tried is getting data from the example sheet and I'm already failing

Any help will be greatly appreciated

#

Example data from sheet:

#

I'm supposed to see the data logged in the execution log right?

it was Logger.log before(not working), and I changed it to console.log,, idk what's the difference

Is there an output thing needed like in C++?

solar abyss
#

I guess I'm not sure how Google Sheets scripting works but it does seem like myFunction isn't being called at all

magic pumice
#

and the function inside of it isn't bein called either

solar abyss
#

@simple mountain where is the function actually being used? Is it maybe used in a cell or something?

magic pumice
#

but that page doesnt have any code examples of calling a function lol

#

it does say "if you run this function, ..."

#

yeah so, myFunction does indeed get called when you run this

#

but logProductInfo isnt getting called, which is why its greyed out by the editor

#

so there a few solutions but one is to delete lines 1 and 14

dry breach
#

I'm struggling to start my homework assignment that's due in two days not because it's a hard problem or anything but because intellij can't find the right files

#

And I didn't do anything to change settings between the last assignment and this one. I literally just cloned from my teacher's starter files on github and the structure is fucked or something

dry breach
#

I was thinking that but I'm going to go ask my professor directly in the morning if we can sort it out

dry breach
#

Talked to my professor and we didn't exactly figure it out but we found a workaround. We tried opening it in VSC and I could open the starter code but I couldn't use the tests. We figured something went wrong in the initial building of the project. We then isolated the starter code files by themselves and added them to a blank project and I was finally able to access both the starter code and the unit tests. He said hes never seen this bug before but he's used to seeing new ones every semester lmao