• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Chris R

Member
You define functions but never call them.

limit = 0

probably needs to look like

limit = ask_limit()

and your ask_limit function needs to verify that the input is a numeric value between 20 and 75. After you get that working you can setup the ask_driver function and verify that the value entered there is above the provided speed limit.
 

SOLDIER

Member
Someone in class gave me a working code, which I followed to create a working Python code for me:

Code:
def main():
    limit = 0
    driver = 0

    limit=int(input("Enter the speed limit: "))
    while limit < 20 or limit >70:
        print ("You must enter a speed limit between 20 and 70.")
        limit=int(input("Enter the speed limit: "))
    driver=int(input("Enter the driver's speed: "))
    while limit >= driver:
        print ("Speed must be over " + str(limit))
        driver=int(input("Enter the driver's speed: "))
    if limit < driver:
        print ("You are driving " + str(driver-limit) + " miles over the speed limit.")

main()

My main problem seems to be the spacing of things. I still have a long way to go, and wish I could find simpler documentation online to understand it.

Only thing left is to create a Raptor flowchart with this assignment. I don't suppose anyone here is well versed with that?
 

-KRS-

Member
Someone in class gave me a working code, which I followed to create a working Python code for me:

Code:
def main():
    limit = 0
    driver = 0

    limit=int(input("Enter the speed limit: "))
    while limit < 20 or limit >70:
        print ("You must enter a speed limit between 20 and 70.")
        limit=int(input("Enter the speed limit: "))
    driver=int(input("Enter the driver's speed: "))
    while limit >= driver:
        print ("Speed must be over " + str(limit))
        driver=int(input("Enter the driver's speed: "))
    if limit < driver:
        print ("You are driving " + str(driver-limit) + " miles over the speed limit.")

main()

My main problem seems to be the spacing of things. I still have a long way to go, and wish I could find simpler documentation online to understand it.

Only thing left is to create a Raptor flowchart with this assignment. I don't suppose anyone here is well versed with that?

What you did wrong previously was that you tried to save a value in the function over_limit, as if it was a variable.
Code:
    over_limit = (speed, limit)
What you should've done there is just remove the equals sign, so that you call the function and pass it the variables speed and limit, like this:
Code:
    over_limit(speed, limit)
That was the main issue.
Furthermore you also called over_limit from within itself. That's a valid thing to do in some cases. It's called recursion. But in this case you shouldn't do that and is something you'll learn further on.
Also when you did this you only passed one value to over_limit by subtracting limit from speed and passing the result on to the function, when the function requires two values.

What you wanted to do in that case is to just remove the line in over_limit calling itself and rewrite the print statement (which also used the function over_limit like a variable) to use the variables directly instead. So the function would look like this:
Code:
def over_limit(speed, limit):
    if speed > limit:
        print ("You are over the speed limit by: " + str(speed - limit))
    elif speed <= limit:
        print ("You are maintaining the speed limit.")

So I think you should read up more about functions. In this case though, functions are a bit over the top and just writing everything in the main function as you've done now is good enough.
 

NotBacon

Member
Hey guys,

How would I start a program where I create a while loop managing the scores of a team?

Literally?
Code:
#include <iostream>

int main() {
}

More generally?
Write down your variables, inputs, outputs, obvious functions, etc.
Then maybe try some pseudo-code:
Code:
func1 () {
    // variable one = 40
    // variable two = 50
    // subtract 3 from variable two until it's less than variable one
    // print "banana"
    // blow up computer
}

Either way, give an attempt of some sort, and then we'll be happy to help with specific questions. Oh and post your code if necessary.
 

Koren

Member
How would I start a program where I create a while loop managing the scores of a team?
Don't start thinking about the program.

Start by thinking about how you'll want to use it. And spend enough time to have a clear idea about what you'll want.


Some interesting questions may:

- How do you enter the changes of the score? One button for each team? Keyboard, pushbuttons, alternate inputs? (I've even used Sony's Buzz device, it can be nice for this)

- Does the sport/game have a more complex system than +1 points? How do you deal with this? Do you want to keep the details in the scoring (maybe for error checking)? Times when scores changed?

- How do you deal with input errors (e.g. points given to the wrong team)? Going back in history? A button for "negative points"? A complete free edition of the current score?

- What do you want to display, just static score or score with "animations", like timers?

- On what kind of hardware do you want to run it?

A simple loop for a static display of scoring could be :


Code:
Initialize variables

while (True) {
    Display score
    Read inputs
    Update score depending on input
}

The update can be a giant "switch" structure depending on the input collected. Input and output totally depend on what you intend to use...
 
Problem 4 Write a complete C++ program that asks the user for a height h and prints a white X pattern (made
of spaces) against a dark background made of Xs.

For example, if the user specied 7 for h, the program would print as follows:

XXXXX
X XXX X
XX X XX
XXX XXX
XX X XX
X XXX X
XXXXX

Answer:
Code:
#include <iostream>
using namespace std;
int main() {
int h;
cout << "Enter a height h: ";
cin >> h;
for (int r = 1; r <= h; r++) {
for (int c = 1; c <= h; c++) {
if ((r == c) || ((r + c) == (h + 1))) cout << " ";
else cout << "X";
}
cout << endl;
}
return 0;
}

What's the trick to tackling these kind of problems?

Each one is can be so different and so finding little details like if ((r == c) || ((r + c) == (h + 1))) cout << " "; becomes like a thing to research for an hour or two
 

Chris R

Member
The trick for most problems like these for me at least is to not think of programming.

If you were asked to do the same problem using a piece of graph paper how would you do it? Is there an obvious patter when that you see when the value of H varies? Once you see yourself doing the same thing over and over again with different values of H you can move onto making the program do it for you, with the user providing the H value.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
The trick is finding a mathematical representation of the pattern.

In this case, it can be solved with a simple coordinate system.

Code:
  1 2 3 4 5 6 7
1 X X X X X X X
2 X   X X X   X
3 X X   X   X X
4 X X X   X X X
5 X X   X   X X
6 X   X X X   X
7 X X X X X X X

Spaces that go top left to bottom right have these coordinates (2, 2), (3, 3), (4, 4), (5, 5), (6, 6). The pattern here is that x == y for all x and y > 1 and < 7.

Spaces that go from bottom left to top right have these coordinates (6, 2), (5, 3), (4, 4), (3, 5), (2, 6). The pattern here is that x + y = 8 for all x and y > 1 and < 7. This "8" can be generated out of "height". If you have trouble finding it, draw a few graphs by hand to see what the underlying pattern is.
 
i was actually thinking about hand drawing a grid before tackling these problems but i wasn't sure that would be a conventional way to go at it

this would definitely be my approach, though and i see now that the user input can have some significance to the patterns that occur, i didn't realize this before and am still cautious about it because it may not always be the case

ill try some different problems and see how it goes
 
Hey guys, I'm having problems with my job search. I graduated recently and had to work on some mental health issues over the summer, but I'm fine now.

However, once I started looking for software engineer jobs I found the descriptions to be unrealistic for the most part. The amount of technologies that I'm expected to know is a bit much.

Right now I'm going through a data structures book, and the Udacity Web Development course, but is there something I'm suppose to be doing in this job search that I'm unaware of? I did go to an Open Source meetup in my city and plan on going to this Tech Week event soon.

Right now I know C++/C, a bit of Android, x86 Assembly, and Python. I made a fitness app in Android.
 
Literally?
Code:
#include <iostream>

int main() {
}

More generally?
Write down your variables, inputs, outputs, obvious functions, etc.
Then maybe try some pseudo-code:
Code:
func1 () {
    // variable one = 40
    // variable two = 50
    // subtract 3 from variable two until it's less than variable one
    // print "banana"
    // blow up computer
}

Either way, give an attempt of some sort, and then we'll be happy to help with specific questions. Oh and post your code if necessary.

Don't start thinking about the program.

Start by thinking about how you'll want to use it. And spend enough time to have a clear idea about what you'll want.


Some interesting questions may:

- How do you enter the changes of the score? One button for each team? Keyboard, pushbuttons, alternate inputs? (I've even used Sony's Buzz device, it can be nice for this)

- Does the sport/game have a more complex system than +1 points? How do you deal with this? Do you want to keep the details in the scoring (maybe for error checking)? Times when scores changed?

- How do you deal with input errors (e.g. points given to the wrong team)? Going back in history? A button for "negative points"? A complete free edition of the current score?

- What do you want to display, just static score or score with "animations", like timers?

- On what kind of hardware do you want to run it?

A simple loop for a static display of scoring could be :


Code:
Initialize variables

while (True) {
    Display score
    Read inputs
    Update score depending on input
}

The update can be a giant "switch" structure depending on the input collected. Input and output totally depend on what you intend to use...
Thanks peep. I already handed in the program, but it was slightly confusing. The next program will be dealing with functions. Oh joy.
 
So uh, I'm a CS student and I'm taking assembly.

I'm doing fine with the homework assignments, cause for the most part it's trial and error with the compile time stuff and debugging, but taking tests and hand writing conversions of MIPS code is fucking killing me and I'm kinda scared I might not pass in the long run, it's kinda scaring the shit out of me.

Anyone have similar issues?
 

Makai

Member
So uh, I'm a CS student and I'm taking assembly.

I'm doing fine with the homework assignments, cause for the most part it's trial and error with the compile time stuff and debugging, but taking tests and hand writing conversions of MIPS code is fucking killing me and I'm kinda scared I might not pass in the long run, it's kinda scaring the shit out of me.

Anyone have similar issues?
I got a D in that class.
 
So uh, I'm a CS student and I'm taking assembly.

I'm doing fine with the homework assignments, cause for the most part it's trial and error with the compile time stuff and debugging, but taking tests and hand writing conversions of MIPS code is fucking killing me and I'm kinda scared I might not pass in the long run, it's kinda scaring the shit out of me.

Anyone have similar issues?

Assembly is one of the first (and honestly, one of the few) mainline CS courses that just require a ton of time and effort. There's no easy way to deal with it. It's like undergrad physics or calculus, it's just a time monster and you have to apply yourself to stay above water.
 
Assembly is one of the first (and honestly, one of the few) mainline CS courses that just require a ton of time and effort. There's no easy way to deal with it. It's like undergrad physics or calculus, it's just a time monster and you have to apply yourself to stay above water.

Yea, that's what I thought.

Issue is I've been basically getting by the past two/three years not studying as much as I needed to and getting A's and high B's. The past year has been pretty tough studying as the reading has ramped up, and the issue is I have an extremely hard time studying and staying on task for, well basically anything.

I've been thinking of scheduling an appointment with a neurologist to get tested for ADD, this is the same shit that has plagued me in high school and I honestly think it's a huge detriment to my education at this point.
 
Yea, that's what I thought.

Issue is I've been basically getting by the past two/three years not studying as much as I needed to and getting A's and high B's. The past year has been pretty tough studying as the reading has ramped up, and the issue is I have an extremely hard time studying and staying on task for, well basically anything.

I've been thinking of scheduling an appointment with a neurologist to get tested for ADD, this is the same shit that has plagued me in high school and I honestly think it's a huge detriment to my education at this point.

You should get an appointment with a psychiatrist if the concentration issues are bad, but remember that you have to decide whether the side effects for psychiatric drugs are worth it.

Just be more self compassionate. I know we're in a hardcore technical field, but you gotta be easy on yourself.
 
Thanks peep. I already handed in the program, but it was slightly confusing. The next program will be dealing with functions. Oh joy.

Functions! I love functions. I also love functions of functions.

Code:
derivative :: (Double -> Double) -> (Double -> Double)
derivative f = \x -> (f (x + h) - (f x)) / h
  where h = 0.000000001 -- Some suitably small number

f x = x ^ 3
g x = 3 * x^2

main =
  let x = 5
      y1 = (derivative f) x
      y2 = g x
  in putStrLn ( show y1 ++
                " is close enough to " ++
                show y2 )
Code:
75.00000265281415 is close enough to 75.0
 
You should get an appointment with a psychiatrist if the concentration issues are bad, but remember that you have to decide whether the side effects for psychiatric drugs are worth it.

Just be more self compassionate. I know we're in a hardcore technical field, but you gotta be easy on yourself.

I mean, to be blunt I feel like I've been struggling my whole life with simple commitment on subjects I care about, but there was pressure externally to not seek treatment and just try to power through it.

I want to be amazing at my field of study, yet I spend an hours on fantasy hockey making sure I have the best line up. I can be really good at golf but practicing madden to win my next league game takes priority :/

I should be finishing my assignment in Computing III yet I'm on GAF, honestly at this point I would rather be pumped full of drugs if it means I can focus on shit I really care about. The list kinda goes on and on, people telling me I could be great if I just applied myself and I never found a way how to.

I mean shit, I'm in the wrong thread talking about this, this is about programming, not whatever shit that possibly fucked in my head!

So to get back on topic, I have a question. I have a homework assignment about making a dynamic array class. Within the design we are given two variables to work with, the size of the array and a pointer of the type the array will store (which is basically an int).

I talked to a kid today who was having trouble so I looked at his code. Turns out we had two completely different interpretations of what the homework as about.

My interpretation was the class was basically a shell and we had to write all the function definitions ourselves, and the array itself was literally just a pointer with us constantly increasing the size and access if the pointer need be (which is what an array is when you boil it down). His was in the constructor he defined an array of the initial size and whenever you had to add to the array he would copy the array + the new element and destroy the old array.

I think I have the correct idea, but now I'm not so sure.
 
I mean, to be blunt I feel like I've been struggling my whole life with simple commitment on subjects I care about, but there was pressure externally to not seek treatment and just try to power through it.

I want to be amazing at my field of study, yet I spend an hours on fantasy hockey making sure I have the best line up. I can be really good at golf but practicing madden to win my next league game takes priority :/

I should be finishing my assignment in Computing III yet I'm on GAF, honestly at this point I would rather be pumped full of drugs if it means I can focus on shit I really care about. The list kinda goes on and on, people telling me I could be great if I just applied myself and I never found a way how to.

I mean shit, I'm in the wrong thread talking about this, this is about programming, not whatever shit that possibly fucked in my head!

So to get back on topic, I have a question. I have a homework assignment about making a dynamic array class. Within the design we are given two variables to work with, the size of the array and a pointer of the type the array will store (which is basically an int).

I talked to a kid today who was having trouble so I looked at his code. Turns out we had two completely different interpretations of what the homework as about.

My interpretation was the class was basically a shell and we had to write all the function definitions ourselves, and the array itself was literally just a pointer with us constantly increasing the size and access if the pointer need be (which is what an array is when you boil it down). His was in the constructor he defined an array of the initial size and whenever you had to add to the array he would copy the array + the new element and destroy the old array.

I think I have the correct idea, but now I'm not so sure.
Nah, you've got it right. He's got it wrong. Don't copy needlessly.
 

Slavik81

Member
My interpretation was the class was basically a shell and we had to write all the function definitions ourselves, and the array itself was literally just a pointer with us constantly increasing the size and access if the pointer need be (which is what an array is when you boil it down). His was in the constructor he defined an array of the initial size and whenever you had to add to the array he would copy the array + the new element and destroy the old array.

I think I have the correct idea, but now I'm not so sure.
I think you're both right. You're doing the right thing, but when you hit the maximum size of the array that you have allocated, you need to copy your array + the new element into a new array of, say, 2x the size of the old array, then destroy the old array. Then increment as normal until you hit the new maximum. Rinse and repeat.

If you copy into a new array for every single append operation (like he did), your program will be very slow due to all the copying. If you never copy into a new array (like you did), your underlying array size can never change, which defeats much of the purpose of having a resizeable array.

So to get back on topic, I have a question. I have a homework assignment about making a dynamic array class. Within the design we are given two variables to work with, the size of the array and a pointer of the type the array will store (which is basically an int).
Only two class member variables? Normally you'd have to keep track of both the size of your underlying array and the number of elements you're currently using, plus the pointer to the array data. Unless, perhaps, this is a language like Java where the array itself can tell you its size.
 

NotBacon

Member
Functions! I love functions. I also love functions of functions.

Code:
derivative :: (Double -> Double) -> (Double -> Double)
derivative f = x -> (f (x + h) - (f x)) / h
  where h = 0.000000001 -- Some suitably small number

f x = x ^ 3
g x = 3 * x^2

main =
  let x = 5
      y1 = (derivative f) x
      y2 = g x
  in putStrLn ( show y1 ++
                " is close enough to " ++
                show y2 )
Code:
75.00000265281415 is close enough to 75.0

Ahhhhhhhhh good ol' formal definitions.
 
I think you're both right. You're doing the right thing, but when you hit the maximum size of the array that you have allocated, you need to copy your array + the new element into a new array of, say, 2x the size of the old array, then destroy the old array. Then increment as normal until you hit the new maximum. Rinse and repeat.

If you copy into a new array for every single append operation (like he did), your program will be very slow due to all the copying. If you never copy into a new array (like you did), your underlying array size can never change, which defeats much of the purpose of having a resizeable array.


Only two class member variables? Normally you'd have to keep track of both the size of your array and the number of elements you're currently using, plus the pointer to the array data. Unless, perhaps, this is a langauge like Java where the array itself can tell you its size.

na, it's C++

this is the header file private declaration

typedef int T; // specify the data type to be stored in the array

class MyDynArray {

private:

// number of elements currently in the array
size_t size;

// array pointer
T *array_ptr;

the setter header is this

// put the element at the position specified by index
// if the position is out of range, increase the size of array accordingly
bool set(T element, size_t index);

note, I didn't write the headers, this is given to us and we need to write code to fit the header and the homework description.

So like I said, my idea was if the client code requested to add a value that as outside the array (the size-1) I would simply add the value of what they wanted to the pointer @ the start of the array + the size of the array (since the array size is always one greater than the actual access of the array, 0 is the start not 1).

However, the issue I run into is if that new pointer value is already accessed by something in the code. Now I know in THIS code that's basically impossible, but in application in real world code that's a more realistic possibility, which makes me think I'm missing a step of validating address of memory before I put in the next array value.

Basically I know the way I'm writing it works for the assignment, but I'm not sure if the TA is going to accept it and grade it the way it is. But it's too late now, shit's was already submitted so now I'm basically trying to figure out if I was wrong!
 

Slavik81

Member
That's a slightly unusual way of defining a resizable array, but I suppose it's just a homework assignment.

Anyways, the pointer you get by adding the size of the array to the pointer to the data is out-of-bounds, and anything beyond that is out-of-bounds too. In C++, you cannot write to memory beyond the end of the array. That is undefined behavior and your program may crash (or may not).

In super-old-school, bare-metal programming environments, you might be able to write to random memory like that, but not in C++.

If the index given is greater than or equal to the size, you will need to create a new array and copy the existing elements into it.

And, actually, you'll need to initialize all the new elements added, too, so you don't end up reading from uninitialized memory during a later resize. Though, that issue is subtle enough that it might go unnoticed unless your program is run under a memory checking tool like Valgrind.
 
Hey GAF, I might have asked this question before, but what's the best book to learn all the concepts of Java? I want to be able to understand why certain concepts work, and understand Java more thoroughly.
 

Water

Member
And, actually, you'll need to initialize all the new elements added, too, so you don't end up reading from uninitialized memory during a later resize. Though, that issue is subtle enough that it might go unnoticed unless your program is run under a memory checking tool like Valgrind.

IMO, every C and C++ course should require student code to be Valgrind clean from day 1, even if that means ramping up the code complexity a bit slower. Learning those languages isn't meaningful unless you actually learning to write them correctly. On a C programming course where I used to work, the automated exercise system did a full Valgrind run on every submission.
 

KimiNewt

Scored 3/100 on an Exam
Kind of random, but: I really like teaching in general and would very much like to get better at (not great atm). Programming is a topic I know a fair bit about and have taught in the past, but I think I can get loads better.

As such, if anyone wants me to teach them (Python or web) one-on-one or in a group (online) and is alright with being my guinea pig, just send me a PM (or message here) and let me know!
Be sure to include what you want to learn, what is your current level, and what is your preferred format.

My main proficiency is Python, having used that as my main language for five years (and taught advanced classes in that, though never from scratch). I've also been a web developer for like three years am cool with teaching that. If someone just wants a short advice session in C/Java/C#/DB or general stuff, that's cool to.
 
here my problem and also how it turned out:
g26Uc2U.jpg


heres the code:
Code:
#include <iostream>
using namespace std;

int main(){
    
    int n;
    cout << "Please enter a positive number.";
    cin >> n;
    
    while (n<1){
          cout << "Your number is not positive. Please enter a positive number.";
          cin >> n;
          }
          
    for (int r=1; r<=n; r++){
        for (int c=1; c<=n; c++){
            if ((r+c)==(n+1))
            cout << "1";
            else 
            cout << " ";
            if ((r+c)==(n+2))
            cout << "2";
            else 
            cout << " ";
            if ((r+c)==(n+3))
            cout << "3";
            else 
            cout << " ";
            if ((r+c)==(n+4))
            cout << "4";
            else 
            cout << " ";
            if ((r+c)==(n+5))
            cout << "5";
            else 
            cout << " ";
            
            }       
        cout << endl;
        }
        system("pause");
        return 0;
}
 

upandaway

Member
You're checking, and printing a character, 5 times per instance of the inner loop, that's why you get 5 characters per tile in the square that gets printed.
 

emb

Member
i have no idea how to make it look the way it should x(
What you ended up with looks way cooler than what they want, for what it's worth.

To get there, think about how many spaces should be at the start of each line, and how that relates to the number the user entered.
 
Hey guys, I'm having problems with my job search. I graduated recently and had to work on some mental health issues over the summer, but I'm fine now.

However, once I started looking for software engineer jobs I found the descriptions to be unrealistic for the most part. The amount of technologies that I'm expected to know is a bit much.

Right now I'm going through a data structures book, and the Udacity Web Development course, but is there something I'm suppose to be doing in this job search that I'm unaware of? I did go to an Open Source meetup in my city and plan on going to this Tech Week event soon.

Right now I know C++/C, a bit of Android, x86 Assembly, and Python. I made a fitness app in Android.

What kind of job are you looking for? The place I work is hiring Android Engineers.

Saw this on Twitter:

Myths and Oversimplifications in Software Engineering.
 
Any Powershell gurus in here? Any recommended resources out there to put me on the fast track? I've been stubborn for the past few years and avoided it; now I must bite the bullet.
 
What you ended up with looks way cooler than what they want, for what it's worth.

To get there, think about how many spaces should be at the start of each line, and how that relates to the number the user entered.

thanks, ive been scratching my head for like an hour but ill see if i can come up with anything by following your clue :)
 

Koren

Member
IMO, every C and C++ course should require student code to be Valgrind clean from day 1
I heartly agree on the principle, but you REALLY need help to set-up the compiling toolchain.

Installing a compiler like GCC and compiling your first program isn't a huge task, but it took me a long time to install and configure valgrind properly. And I had 10+ years of C/C++ behind (and twice that of programming).

setting up a set of
- editor
- compiler
- debugger
- common libraries
- valgrind-like software
is really not beginner-friendly. And I don't think you can properly learn to code just with the hours at school...


I'm quite happy that prep schools here use Python and Caml Light as languages for learning CS because both have complete environment packaged in a single installer, which make students lives easier... (although I'd probably have chosen ADA as first language)
 

poweld

Member
Hey guys, I'm new to programming and just started taking a programming course at my university that teaches python. We have an assignment that requires us to program a version of Pong using pygame in python3.

Currently, I am trying to sort out the collision pattern of the ball. I've been able to get the ball to detect the borders of the window by using this code segment (with center being the center point of my ball):

if center[0] <= radius :
speed[0] = -speed[0]

if center[0] >= windowWidth - radius :
speed[0] = -speed[0]

if center[1] <= radius :
speed[1] = -speed[1]

if center[1] >= windowHeight - radius :
speed[1] = -speed[1]

The problem is that I haven't been able to get the collision pattern working between the paddles and the ball. I've tried a few variations of something similar for the paddles, but I have had no luck. I have the paddles stored as pygame.Rect().

I know this may be simple for most people, but I'd really appreciate any help or guidance!
 

Fishlake

Member
Hey programming GAF. Just wanted to share the past 2 hours of debugging I've done.

My DES program for Computer Security was returning 0 on some variables when by all accounts they looked correct. They were declared at uint16_t instead of 64...

I'm both thrilled and disappointed in myself that this was the problem there. :D.
I made sure to ctrl F uint16_t and and change them to 64. Only one other occurred.
 
setting up a set of
- editor
- compiler
- debugger
- common libraries
- valgrind-like software
is really not beginner-friendly.

Spoken like someone who doesn't use Windows. ;-) The solution is obvious: schools should start using Windows. Everything you mentioned can be done with about a 5 click install.
 

Koren

Member
Spoken like someone who doesn't use Windows. ;-) The solution is obvious: schools should start using Windows. Everything you mentioned can be done with about a 5 click install.
That's strange, because I think the exact opposite... I've had huge troubles because my laptop was on windows, even with Cygwin being a huge help, when my Linux machine was set up in minutes.

I'd be curious of the best way to have gcc, gdb and valgrind running in a couple of automatic install. Truly interested, actually...


I've even nightmares each time a fresh windows install forces me to go through the installation, configuration, and often rebuild of SDL, FLTK, Boost, Blitz, QT, etc. Especially because I want a mingw toolchain in Cygwin, but even without that...


Edit : virtually all the schools here I worked wirh have a dual-boot with Windows and Linux (or plain Unix)
 
At the moment I'm learning Python 3 and going through a book for it.

The book just went through list comprehensions and list/tuple data types/structures.

So they have a summary at the end where they ask a bunch of question to test your knowledge. One question that I want to make sure on is as follows:

Question said:
Come up with three different ways to create a list of 25 ones without simply typing the 25 ones.

So obviously my first solution would be:

Code:
my_list = [1]*25

And my second solution would be:

Code:
my_list = []
for i in range(25):
	my_list.append(1)

So at the moment I'm struggling to come up with a third solution, I assume they want me to use a list comprehension in some way but as far as I understand it the comprehension can only add elements from a collection (range(25)) to the newly generated list. So I can't say something like:

Code:
[i=1 for i in range(25)]

Because that would be trying to reassign the value of i which would be numbers 0-24.

Am I just thinking about this wrongly or missing something simple? Do they even want me to use a comprehension at all for this?
 

Massa

Member
Spoken like someone who doesn't use Windows. ;-) The solution is obvious: schools should start using Windows. Everything you mentioned can be done with about a 5 click install.

It's far easier to install a development environment with apt-get on Linux than going through multiple setup programs on Windows.
 

peakish

Member
So at the moment I'm struggling to come up with a third solution, I assume they want me to use a list comprehension in some way but as far as I understand it the comprehension can only add elements from a collection (range(25)) to the newly generated list. So I can't say something like:

Code:
[i=1 for i in range(25)]

Because that would be trying to reassign the value of i which would be numbers 0-24.

Am I just thinking about this wrongly or missing something simple? Do they even want me to use a comprehension at all for this?
Your solution is very close. Note that you don't have to use your assigned variable in a list comprehension. Hence you can do
Code:
[1 for _ in range(25)]
where the underscore is a nice way to not even do any variable assignment inside the comprehension.
 
Any Powershell gurus in here? Any recommended resources out there to put me on the fast track? I've been stubborn for the past few years and avoided it; now I must bite the bullet.

Powershell's kind of odd. The most helpful resource for me was the Hey, Script Guy! blog on msdn's website, some of msdn's getting start guide, and powershell itself. Because their commands are logically laid out in a verb-noun structure, I would use get-verb to find where to start, get-command <verb>-* to find sub commands, then get-help <verb>-<noun> --examples to see how to use it.
 
Your solution is very close. Note that you don't have to use your assigned variable in a list comprehension. Hence you can do
Code:
[1 for _ in range(25)]
where the underscore is a nice way to not even do any variable assignment inside the comprehension.

Ahhh I see, I didn't even think of that! Thanks!

So the same thing would work if I used i instead of the underscore and still just had the number 1 as the expression?

Also I've never seen an underscore as a variable before...can you perhaps explain a little bit more about that convention in Python? Thanks!
 

peakish

Member
Ahhh I see, I didn't even think of that! Thanks!

So the same thing would work if I used i instead of the underscore and still just had the number 1 as the expression?

Also I've never seen an underscore as a variable before...can you perhaps explain a little bit more about that convention in Python? Thanks!
Yes, you could have used i or whatever instead of _. But it's common practice in Python (and at least one other language) to use _ as a throwaway variable. In that way, a glance at the expression tells you that the assignment isn't important (and you won't have to worry about it rebinding another variable by mistake).
 
Top Bottom