Noob to Hacker

A narrated quest - From ignorance to bliss

Recent Posts

  • All Moves for Each Pokemon Type (Fire, Water…) in Pokemon Go

    All Moves for Each Pokemon Type (Fire, Water…) in Pokemon Go

  • The best Pokemon Go tips and advice

    The best Pokemon Go tips and advice

  • Complete List of Moves and Attacks for Each Pokemon in Pokemon Go

    Complete List of Moves and Attacks for Each Pokemon in Pokemon Go

Day 28 – Program to convert 1 column csv file into a txt of all values separated by comma

June 4, 2016 by Noobtohacker

Day 28 – Program to convert 1 column csv file into a txt of all values separated by comma

Creating a program to convert excel column to a comma separated string or list

Well, coming back to programming after more than one month, and to Python after more, definitely took time. But in just these hours I was able to refresh my memory and create a program that solves a problem we had in the company. The problem solved is how to convert an excel column into a comma separated list.

Imagine that you have an excel column, with 20.000 rows, each row is a number of 6 digits for example… well, what if I want to create a string with all those numbers one after another separated by a comma?

An excel possible solution

Well, there is a technique you can use on excel, which is basically

Values to string separated by comma
Values to string separated by comma

Now, this technique has a big limitation. An excel cell can only contain a maximum of 32,767 characters,which sometimes really is not enough for the kind of row numbers we want to convert into a string of values separated by comma.

Usually with the kind of data we use, after 3500 rows or so, the cell gets fully filled and values stop copying.  I needed to find a solution, so I did. I created a program with python.

Finding a way to list unlimited values separated by comma.

So I finally made it, and here is the final program:

0604-program to accomplish this

Filed Under: Learning Computer Science

Day 28 – I am back one month after

June 4, 2016 by Noobtohacker

So I am back. Blackout lasted 1 month almost. Crazy how easy it is to fall into this kind of thing.

Now I have the feeling I have forgotten everything… and seems like all the previous blog was written by someone else! It’s so interesting how easy it is to lose track.

Anyway,  I am back and I am gonna make sure I am back for another big push.

Best.

Filed Under: Learning Computer Science

Day 27 – Time out – Dark period I wanted to precisely avoid

April 24, 2016 by Noobtohacker

So I have not posted anything in 10 days or so. This is exactly the kind of “dark period” that I was fearing. I don’t want to lose momentum.

Unfortunately things at work got too busy, I had to attend a wedding that kept me out of station for almost 6 full days and too much stuff going on, and for 1 or 2 months it’s gonna remain like that. After then, I might finally have a lot of time to speed things out quite a lot. Now, what should I do? Should I just stop and re-start after 2 months? Or should I keep going, knowing that dedicating time to this might unfocus me slightly from work, now that we are at a critical period?

Well, I think in a way these are all excuses… one thing I have definitely noticed in this last 10 days, is that the less coding I do, the more I waste time. I downloaded “Plants vs Zombies”, and must have definitely put in there 10 hours or so totally. Finished pretty smoothly. And then as usual started to check videos on YouTube to see how far people are taking it… and realized that if I want to reach top level there I would need to dedicate more hours. So I have deleted the game. Just now as I write these lines as a matter of fact. 

Hence, I must try to keep going. Stopping now for 1 or 2 months is not gonna solve anything. 

One last thing… is not that I have not been doing nothing at all. 

Actually, in those moments where you really feel like doing nothing… instead of playing even more games or waste time in other similar ways, I am watching the whole playlist of videos of this channel: lcc0612 – Super interesting channel.

Filed Under: Learning Computer Science

Day 26 Structs, Pointers images and more on Hexadecimal.

April 11, 2016 by Noobtohacker

Day 26 Structs, Pointers images and more on Hexadecimal.

Its great to see how files at the end of the day are just 0’s and 1’s like everything else! And in featured image you can see the signature of a bitmap image.  A Jpeg however has a much smaller one, just three bytes at the beginning of the file.

As you can see the bytes are written in Hexadecimal. And very interestingly, an hexadecimal is pre-poned by “0x” to know it is coming.

0411-JPEG Signature2 si

 

Structs in C

Very interestingly, seems that you can create your own datatypes on C. And this very much reminds me of the concept of a “Class” and Objects… it is pretty much the same.

Imagine you want to create and store some data for different students. This is a way you could do it with lists, but of course has immense limitations (In the case below you only have 3, what if you need more?):

0604-nostructsproblem

  • So it seems that in C you can create your own, and by convention you usually write these in a .h file.

0411-struct2

 

You can see below different things:

  • It is good conventions to declare constants by using #define type () like below it does with “STUDENTS 3”
  • It is good convention to write the constant in capital letters
  • As you can see in the #include for structs.h, it is using “” instead of <>. This is because that file is in the current directory. So to include a file in the current directory, one uses quotes “”.

As you can see we called “structs” where the “student” datatype is contained.

So below he is saying, give me a variable called students, each of type students, and specifically give me 3 of those in my array.

On line 17 you can see there is a “.name” or “.dorm”. Those are basically things inside of each student.

0411-struct3

 

And below you can see how a CSV file is created in C, to store whatever a program outputs.

0411-create file

 

Pointer + strcmp

A pointer is an “address in memory”. When I say char* s = GetString()… then this doesn’t mean give me a string. It means, give me a variable who’s purpose in life is to store an address because I am about to put the address of a string into it.

And GetString does not return a string. It returns an address. The address of the first character in some string it has gotten.

Below you can see a good convention. Checking if s or t are NULL. When s or t return something different from an address… it returns NULL, that is an error. Something went wrong.

What can happen is that maybe string stored is so big that there is no memory to hold it.

C comes with a function that does that iteration

  • strcmp (pointer1, pointer2)
  • It distinguishes between lower case and upper case

This basically goes to those two addresses in memory and checks each character until reaching the \0.

0411-checkifnull

 

Important below, what is happening is that t is getting the address in memory of s, not the “string” we wrote in it.

0411-line16

 

Below you can see that t doesn’t get “mom” it gets 1. The address in memory.

0411-line162

 

And below you can see that we are capitalizing first letter of the array:

0411-line163

 

And now something important but complex. This code basically it says in line 16 the following.

First create a pointer t which stores  an address in memory. Then Malloc means “memory allocation” which is basically “give me a chunk of memory”.

We are saying, give me the strlen of s (which is 3), then I add 1 to account for the \0, (allocate 4 bytes, so need the length of the string + 1) and just for good measure we say, we * for “sizeof” which basically tells us the size of a (char) in that case. (I guess this is a safety measure). sizeof it tells you number of bytes needed for a certain datatype. (btw, sizeof(char) is usually 1).

And now basically we copy each character of s into t through a loop. So that now I have 2 copies of original.

Lastly you can capitalize the first letter of the copy you made!

0411-line164

 

And finally we can have an even more advance way of writing this.

*t is basically saying, go to the following address. “t”.

Pointer arithmetic (math with addresses).

So basically *(t + i) is going to each position after t… same as t[i].

0411-line165

 

So for SWAP function we did in previous class, instead of passing the values of x,y  which actually become a and b as copies, you can pass the addresses… like below:

0411-address

 

Inside of function, start means go inside… so *a means whatever is inside that integer address. (Memory knows it is an integer as we have already defined).

And remember, to that function swap we need to pass the addresses, not the values. You do that by suing the & character like below:

0412-address

Below a program declares x and y who are addresses of integers (pointers). Then we allocate enough memory to store an int, and store that memory address in x. Then go to address in x, and put number 42. And *y says go to address in y… (but this is dangerous as I have not put anything in y!!)

Things they point to are “pointees”.

Initially pointers don’t point to anything.

To allocate a pointee, you need to first allocate it with the “malloc(sizeof(int));” term, for a creation of an integer pointee. And then you can point x to that by doing x = (the formula before).

So once already have the pointee we can dereference it by using *x which etches that value on the memory we had allocated.

When you do y = x; then y will be also assigned to the same pointee than x was. And if then you do *y = 13, basically you are changing the pointee stored value to 13, so if now you print x I guess it will show 13!

0413-address

 

Filed Under: Learning Computer Science

Day 25 Switching Variables in C – First mention of pointers

April 10, 2016 by Noobtohacker

Day 25 Switching Variables in C – First mention of pointers

So basically he explained something that actually has happened 2 or 3 times already in problem sets. If I want to switch two variables, usually I need to create a temporary variable to hold a value to make the switch possible.

So to switch a to b, you need to do:

tmp = a

a = b

b = tmp

And in class they used an analogy with 3 cups and need to switch from one to another:

swtich0

Then seems that you can use XOR (^) to swap 2 variables. You can test it by applying XOR to each bit, bit by bit… you will see that it actually works :D!

switch values

Important, also learned that you can represent your computers memory like below. Stack is chunck of memory that functions have access when they are called.

stakheap

Interesting that environment variables (of main) take a chunk of space, but functions take a different one. When I pass variables to a function, those are a COPY of the original. So what if I want a function to affect the  space of the environment variables?

bfswap

Below is what happens after swaping a and b on function.

bfswap2

This is a very interesting example. Somehow something is wrong.

The interesting thing is that what is stored in any “string” variable, is not the string itself, its the address in memory of that string.

Actually “strings” doesn’t exist. Its just a synonym, for char*. The * is a “pointer”, an “address”.  So from now on, instead of calling the type “string”, we will put “char*”

typed different things

Filed Under: Learning Computer Science

  • « Previous Page
  • 1
  • 2
  • 3
  • 4
  • …
  • 9
  • Next Page »