• 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

Koren

Member
Can I recommend creating a Docker container for development?

For example taking the Python container as a base (https://hub.docker.com/_/python/) and installing the Python module that communicates with the hardware through that.

https://docs.docker.com/docker-for-mac/
All suggestions are really welcome... If not for this project, it could be interesting for others.

Though I'm not sure I see what docker is... A kind of virtual machine? No chance it limited commnication with hardware?

And once you've installed Python inside it, you have a kind of command-line tool that allow to run pip, and that support compilation of code?

Also, how "open" is the project?

In any case, many thanks, I'll look into it (and probably find answers to several of thise questions, but on a phone, it wasn't easy to see the whole picture behind this, sorry)
 
All suggestions are really welcome... If not for this project, it could be interesting for others.

Though I'm not sure I see what docker is... A kind of virtual machine? No chance it limited commnication with hardware?

And once you've installed Python inside it, you have a kind of command-line tool that allow to run pip, and that support compilation of code?

Also, how "open" is the project?

In any case, many thanks, I'll look into it (and probably find answers to several of thise questions, but on a phone, it wasn't easy to see the whole picture behind this, sorry)

Docker is an application container engine; a certain kind of a virtual machine. It's hardware and platform independent and rather lightweight solution. It can do anything that a real machine can do, including any kind of communication with any kind of machine.

For example if you that Python container, it's a container where Python is installed on top of (most likely) Debian container; a fully fledged linux machine with Python preinstalled (like most Linux distributions in general, but you can pick the version you want and so on).

For example, first install Docker for Mac on your Mac. Then say you have this Dockerfile on your local folder and your hardware thing is available on apt-get. You also want to run `hello.py` by default.

Code:
# hello.py
print("Hello, World!")

Code:
FROM python # take python as a base container

RUN mkdir /usr/app # create a folder
WORKDIR cd /usr/app # cd into it
COPY .  /usr/app # copy your local filesystem files
RUN apt-get install your-hardware-thing # install the hardware thing
ENTRYPOINT python hello.py

Now you can build your container with

Code:
docker build -t my-container .

You'll see the steps being applied

And then you can run your container:

Code:
docker run -it my-container

You'll see "Hello, World!" and then the program will return you to the cmd line. But you can also override the ENTRYPOINT

Code:
docker run -it my-container /bin/bash

(use exec instead of run if your container is still running)

After which you'll end up in a native bash command line inside your container, where you can run any commands that you'd like.

Docker itself is completely free and open source, available here: https://github.com/docker/docker. It's used widely in the dev industry.
 

Koren

Member
That's REALLY interesting. Curiously, that's the first time I heard about it. I'll definitively try it (probably for more things than my current "problem").

Many thanks both for the suggestion, and for the time you took to explain the details. I really appreciate it.
 

vypek

Member
So I'm starting to mess around with Python and I'm a bit confused on something. I have some lists and integers that I am making use of in some functions (they are declared before I define the functions). I can freely work on the lists in the functions using append but for some reason the function doesn't like the integer claiming it was referenced before assignment.

What exactly is happening here that I can operate on the lists but += 1 is causing an error?
 

Jokab

Member
So I'm starting to mess around with Python and I'm a bit confused on something. I have some lists and integers that I am making use of in some functions (they are declared before I define the functions). I can freely work on the lists in the functions using append but for some reason the function doesn't like the integer claiming it was referenced before assignment.

What exactly is happening here that I can operate on the lists but += 1 is causing an error?
You're going to have to post some code here.
 

red capsule

Member
hello friends,

was wondering if someone can confirm whether or not i'm doing the act of swapping pointers correctly

context:
- there's a struct name 'Person' with last name and first name member variables
- I declared an array of pointers (the "original array") which will point to their own individual 'Person' struct
- i created 2 separate array of pointers pointing to the "original array"
- one array will sort based on last names (alphabetical order), the other will sort based on first names (alphabetical order)
- will have to do this without manipulating the contents of the original array (has to stay the same)

here's my attempt on it which I'm having some doubts on :(
I will be using sort from the algorithm STL btw (also, C++)
Code:
#include <algorithm>
struct Person
{
string lastname, firstname;
Person(string ln, string fn) : lastname(ln), firstname(fn) {}
};

class DataBase
{
private:
	Person P*[MAX];  // assume MAX is like 10
public: 
	void printList(); // prints original list, prints list sorted by last names (alphabetical order), prints list sorted by first names(alphabetical order)

// custom compare functions for sort
	static bool compareLastN(Person* a, Person* b) { return (a->lastname < b->lastname); }
	static bool compareFirstN(Person* a, Person* b) { return (a->firstname< b->firstname); }
};

Code:
void DataBase::printList()
{
// "original array"
	P[0] = new Person("johnson", "dwayne");
	P[1] = new Person("vorhees", "jason");
	P[2] = new Person("walt", "disney");
// the 2 seperate arrays
Person* LastNSort[MAX];
Person* FirstNSort[MAX];

for (int i = 0; i< 3; i++)
{
lastNSort[i] = *(P + i);
FirstNSort[i] = *(P + i);
}
	sort(lastNSort, lastNSort + 3, compareLastN);
	sort(FirstNSort, FirstNSort + 3, compareFirstN);
}
 

vypek

Member
You're going to have to post some code here.

I would up solving it close to when I posted. Python apparently assumes a variable is local unless explicitly told it's not so in the function definition I used the global keyword. Gonna look into that more tomorrow morning on why that is how it is. Will also post some similar code to what I had
 

Koren

Member
I would up solving it close to when I posted. Python apparently assumes a variable is local unless explicitly told it's not so in the function definition I used the global keyword. Gonna look into that more tomorrow morning on why that is how it is. Will also post some similar code to what I had
Python interpreter first look into the "local"/function "dict" for a matching name, and if it's unable to find it there, he look into other "dicts".

You don't actually need to declare a name "global" if you only read it/mutate it. But you can't re-assign it. I think it's a security measure, even if it still seems dangerous to me.

It can also be tricky... For example
Code:
L += [1]
and
Code:
L.extend([1])
are virtually the same thing, but if L is not defined inside the function, the latter should work without "global L", the former may not (depending on Python interpreter).
 

Makai

Member
So much effort for pretty much nothing

BPXEJo1.gif
 

vypek

Member
Python interpreter first look into the "local"/function "dict" for a matching name, and if it's unable to find it there, he look into other "dicts".

You don't actually need to declare a name "global" if you only read it/mutate it. But you can't re-assign it. I think it's a security measure, even if it still seems dangerous to me.

It can also be tricky... For example
Code:
L += [1]
and
Code:
L.extend([1])
are virtually the same thing, but if L is not defined inside the function, the latter should work without "global L", the former may not (depending on Python interpreter).

Ah thanks. Thats really interesting. Thinking in a more Python way really throws me off for some simple stuff. Utilizing whitespace is interesting but still need to get used to it
 

Makai

Member
A large part of the internet became inaccessible today after a botnet made up of IP cameras and digital video recorders was used to DoS a major DNS provider.

Amazing
 
Ok so in PHP I know how to perform a Select query to pull data from a mySQL database and then generate a table on the fly using that data.

How do I do this in nodejs?

Code:
 <?php
if(!($stmt = $mysqli->prepare("SELECT patient_lab_test.pid, patient_lab_test.ltid, fname, lname, lab_test.name, lab_test.description,
lab_test.unit, lab_test.low, lab_test.high, patient_lab_test.result, patient_lab_test.test_date
FROM patients INNER JOIN patient_lab_test on patients.id = patient_lab_test.pid
INNER JOIN lab_test on patient_lab_test.ltid = lab_test.id"))){
	echo "Prepare failed: "  . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
	echo "Execute failed: "  . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->bind_result($pid, $ltid, $fname, $lname, $name, $desc, $unit, $low, $high, $res, $date)){
	echo "Bind failed: "  . $stmt->errno . " " . $stmt->error;
}
while($stmt->fetch()){
 echo "<tr><td>" . $pid . "</td><td>" . $ltid . "</td><td>" . $fname . "</td><td>" . $lname . "</td><td>" . $name. "</td><td>" .
 $desc . "</td><td>" . $unit . "</td><td>" . $low . "</td><td>" .
 $high . "</td><td>" . $res . "</td><td>" . $date . "</td></tr>";
}
$stmt->close();
?>
 

Pokemaniac

Member
Ok so in PHP I know how to perform a Select query to pull data from a mySQL database and then generate a table on the fly using that data.

How do I do this in nodejs?

Code:
 <?php
if(!($stmt = $mysqli->prepare("SELECT patient_lab_test.pid, patient_lab_test.ltid, fname, lname, lab_test.name, lab_test.description,
lab_test.unit, lab_test.low, lab_test.high, patient_lab_test.result, patient_lab_test.test_date
FROM patients INNER JOIN patient_lab_test on patients.id = patient_lab_test.pid
INNER JOIN lab_test on patient_lab_test.ltid = lab_test.id"))){
	echo "Prepare failed: "  . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
	echo "Execute failed: "  . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->bind_result($pid, $ltid, $fname, $lname, $name, $desc, $unit, $low, $high, $res, $date)){
	echo "Bind failed: "  . $stmt->errno . " " . $stmt->error;
}
while($stmt->fetch()){
 echo "<tr><td>" . $pid . "</td><td>" . $ltid . "</td><td>" . $fname . "</td><td>" . $lname . "</td><td>" . $name. "</td><td>" .
 $desc . "</td><td>" . $unit . "</td><td>" . $low . "</td><td>" .
 $high . "</td><td>" . $res . "</td><td>" . $date . "</td></tr>";
}
$stmt->close();
?>

Node doesn't have built in mysql support, so you'll have to find some library (preferably on npm) to interface with it. The exact code will depend on the library you choose. I've never tried to use that exact combination of software, so unfortunately I don't have any recommendations for which package to use.

As for displaying that data in a table, that is also going to depend on how you're rendering your pages. I'm personally rather fond of templating using pug (formerly known as jade), but it is definitely not the only option.
 
Hey programming Gaf! I got an associates degree in cis about 7 years ago or so but never pursued anything with it and like most schooling for me I only ever learned enough to pass the classes. I'm in between jobs at the moment and thinking about getting back into programming but want to start fresh. I've heard people mention Python as a good language to learn as well as Java and all variations of C. Does anyone have any recommendations for useful books/guides to learn how to code?

Any help would be appreciated!

Thanks all!
 
Wondering if anyone can help me with an assignment question. I am a bit stumped.

Say I have something like
physical address 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
content 3 25 5 23 45 8 28 2 9 7 18 12 0 4 29 8 5 60 61 62

Say a word size of a page is 4 and the page table is of size 8

Suppose I have a logical address in 3. I am kind of wondering how I map this to a page.

From my understanding, I thought it was the virtual address - n, where n is the exponent needed to get to the word size, dealing with powers of 2. So, here, n would be two. This would mean that 3 maps to 1.

However, after thinking over it again, I am wondering if I am suppose to take the division of the logical address / word size, so 3 / 4 for the first, which is 0.

I am wondering which approach is accurate.

Thank you.
 
Hey programming Gaf! I got an associates degree in cis about 7 years ago or so but never pursued anything with it and like most schooling for me I only ever learned enough to pass the classes. I'm in between jobs at the moment and thinking about getting back into programming but want to start fresh. I've heard people mention Python as a good language to learn as well as Java and all variations of C. Does anyone have any recommendations for useful books/guides to learn how to code?

Any help would be appreciated!

Thanks all!

Start with Harvards CS50, which is their introduction CS class, aimed at beginners (but at Harvard level so it is demanding and doesnt coddle you too much). It starts with C and later delves into Python. The site is a bit of a mess in my opinion but you can find everything you need there, from the syllabus to the lectures, which are great.
 

Koren

Member
Wondering if anyone can help me with an assignment question. I am a bit stumped.
Read it three times, and still don't sure I understand... I apologize in advance if I'm way off and state obvious things, but I'll try...



Assuming n=4 items per page, 20 items would be dispatched like this:

Code:
                    *
|  0  1  2  3 |  4  5  6  7 |  8  9 10 11 | 12 13 14 15 | 16 17 18 19 |
    page  0       page  1       page  2       page  3       page  4

Assuming everything is zero-indexed, item #5 for example is item #1 on page #1.


The index i_p of the element in a given page is reset for each n elements. So the index i_p on a page is the remainder of the integer division of the overall index i by the number of elements n on a page.

The corresponding page p is the quotient of the integer division of i by n

Meaning:

i = p * n + i_p

and thus (the exact notation can depend on the language):

i_p = i % n

p = i // n


If the number of items per page is a power of 2, like 2^k, you can also use (faster) :

i_p = i >> k

p = n & mask where mask = (1<<k)-1



The number n of items per page is, obviously, the division of the page physical size by the element physical size (using the same unit, e.g. bytes)
 

Koren

Member
I'm in between jobs at the moment and thinking about getting back into programming but want to start fresh.
Why do you want to learn programming? Related to jobs? To code for fun (if so, which kind of things)? To code to automate some everyday tasks? Because you're interested in pure algorithmics?


You'll probably find as many suggestions as a start as there is programmers... The problem with languages is that those that are quite good to learn coding are also not-that-popular languages, and quickly lack the librairies to do large projects, and sometimes also ressources. Languages with a lot of supports aren't always the best for learning.

There's also a matter of taste...


I'd suggest something like Ada as a language to learn algorithmics, but for anyone that want to do something nice quickly, that won't work.

Python is nice, because it's close enough to natural language and logical so that you can easily get the trick and do interesting things. They'll be a couple surprises along the road, though*. And I wouldn't suggest Python for very large projects.

C is decent, but the close-to-the-metal philosophy make it annoying at times, since you'll have to redo a lot of things by hand. It's probably a must-learn, but not always the first thing to do. STL structures from C++ makes things more manageable but I'd avoid the bulk of C++ at the beginning.

I'd stay far away from Java, but that's me ;)

C# is only very loosely linked to C (closer to Java, in fact, but better**), despite the name. It can be a good start, I think.

F# has a completely different philosophy (functional programming), but it's also a possible interesting starting point. I'd say you need to learn a functional language sooner or later if you're serious into programming, in any case. I used to suggest (O)Caml, but F# has the advantage of the ecosystem now, and they have a lot in common. It can feel strange, but many students I've seen that learn programming for the first time (Python, then Caml) don't want to go back to Python once they've tried Caml. Some others don't like it. You'll have to try to know ;)







* Pass-by-assignation is nice, but not the easiest thing to fully understand... For example, this code
Code:
L = [ 1, 2 ]

def f(L) :
    L[0] = 3
    L = [ 4, 5 ]
    L[1] = 6
    return L

print(L, f(L), L)
is a common way to make life harder for beginners... (there's several traps if someone want to try ;) )

** yes, that's a troll, but I assume it ;)
 
Hey guys, this is probably a bit disconnected from the main topic, but I'm a programmer with a Bachelor's in Computer Engineering who is currently working, and people have been telling me that for my Master's, I should actually get an MBA. For the short term, I expect to continue doing programming jobs, and this is more future-proofing. So my questions are:
* What business major would be good for a programmer to study?
* Is it recommended to study full-time, or is it recommended to continue working while taking a part-time degree? I'm in the California Bay Area, so plenty of schools offer part-time degrees.
 
* Pass-by-assignation is nice, but not the easiest thing to fully understand... For example, this code
Code:
L = [ 1, 2 ]

def f(L) :
    L[0] = 3
    L = [ 4, 5 ]
    L[1] = 6
    return L

print(L, f(L), L)
is a common way to make life harder for beginners... (there's several traps if someone want to try ;) )

I'm bored so I'll give it a shot!

Code:
L = [1, 2]
#Object of type 'List' created in memory. Contains values: 1, 2 at index 0 and 1
#Object is assigned the alias 'L' in the namespace.

#Object of type 'Function' created in memory. Assigned to the alias 'f'.
#Takes a parameter called 'L' this new 'L' is inside the function's local namespace
def f(L): 
    L[0] = 3
    L = [ 4, 5 ]
    L[1] = 6
    return L

print(L, f(L), L)

1. The first L is before the function call so the first value printed will remain the same as when it was originally assigned. [1, 2]

2. Function 'f' is now called with our L list as a parameter. Because Python is pass by Object the L that is listed as our parameter in the function is actually the same object in memory as the L list we defined outside the function.

3. Lists are mutable so when we say L[0] we are changing the L object that already exists in memory. Remember this is the exact same object we defined outside the function.

4. We reassign the local function variable L to be the list [4, 5] this creates a new list object in memory with the values 4 and 5 and assigns it to the alias 'L' that exists in the namespace of the function object.

5. Now we have two variables named 'L'. One exists as a global variable outside the function in the global namespace. It's current value is [3, 2] at this point in time. The second 'L' variable is local to function. Meaning it is a part of the function's namespace. It's current value at this point in time is [4, 5]

6. Because Python's scoping rules work in a specific order: Local, Enclosing, Global, Built-In this means that Python when presented with a variable name will first look in the local function before looking outside it to the global L variable. This means that when we say L[1] = 6 we are now talking about the L list object that exists inside the function's namespace and not the L object that is in the global namespace. So this changes the local L list object to now contain [4, 6]

7. We print the return value of the local L list object. Which is now [4, 6]

8. We print the value of our global list object L, which was mutated as a side effect of the function call. The current value of L when printed here is: [3, 2]

In other words the printed output will be: [1, 2] [4, 6] [3, 2]
 

Koren

Member
I'm bored so I'll give it a shot!

In other words the printed output will be: [1, 2] [4, 6] [3, 2]
Near perfect... You have the most important part correct.

Now, the "print" part was just me playing nasty.

It's actually [3, 2] [4, 6] [3, 2]

The trick is that, even if the first L given to the print corresponds to [ 1, 2 ], the first element of the *vargs in the print designate the list itself, not a copy.

The python interpreter evaluate all elements to fill the elements in the *vargs for the print, and between the time you provide the first L and the moment you begin the printing, nothing prevents you from changing the content of the list. That's exactly what happen.


It can get even dirtier... Nothing prevents you to do some side-effects when you convert an object into a string, a process that print will have to perform.

For example (don't do it, it's wrong on many levels ;) ):
Code:
class foo() :
    def __init__(self, L) :
        self.L = L
    
    def __str__(self) :
        res = str(self.L)
        self.L.append(3)
        return res
    
    def append(self, value) :
        self.L.append(value)


def f(L) :
    L.append(2)
    return L

L = [ 1 ]

P = foo(L)

print(L, f(P), L)

will probably produce [1, 2] [1, 2] [1, 2, 3]

Indeed, f(P) will append 2 to L, which will be seen in all elements. But the 3 is appended during the conversion of the second object into a string, so the 3 only appear when you print L the second time.

I said "probably" because I don't think print gives any warranty that objects passed as arguments are converted to strings in the left-to-right order. For memory reasons, I can't see any implementation doing otherwise, but still, I don't think the result is specified by Python specifications.


At this moment, many people begin to think that pure functional programming, with only constants, is not that a bad idea after all ;)
 
Near perfect... You have the most important part correct.

Now, the "print" part was just me playing nasty.

It's actually [3, 2] [4, 6] [3, 2]

The trick is that, even if the first L given to the print corresponds to [ 1, 2 ], the first element of the *vargs in the print designate the list itself, not a copy.

The python interpreter evaluate all elements to fill the elements in the *vargs for the print, and between the time you provide the first L and the moment you begin the printing, nothing prevents you from changing the content of the list. That's exactly what happen.

My entire life is a lie.... :p

I was sure I had everything correct, I didn't even think about the print statement!

So if I understand this correctly the print statement is just a function object.

The print function takes in 3 arguments to it's parameters, these objects are added to the function's local namespace.

Because Python is pass by object all 3 'L' variables passed are the same object in memory.

Before it actually performs the actual display of text to the screen it must first evaluate all of the parameters to find their final values. So even though the 1st L object had a value of [1, 2] when it was passed in, after the function is invoked it mutates this L object as a side effect and both the first 'L' parameter and the last 'L' parameter are pointing to the same object in memory so that object is mutated by the 'f' function.

By the time the text is finally displayed to the screen the mutable List object in memory has now been mutated to contain final values of [3, 2]

Because both 'L' alias point to the same object the resulting output is [3, 2] [4, 6] [3, 2]

I completely forgot that the built-in functions are objects too and so are governed by the same rules as everything else. Just such a sub conscious thing when you see print()

That was dirty... hah. Just means I need to pay more attention to keep the rules in mind at all times. I finally thought I was getting good :(
 
Yessss

VfPsOTt.gif


This is from a tutorial but it took forever to generalize the code to use arbitrary meshes.

On a less facetious note, looking at the teapot again, there seems to be something...off with it.
Like, the positions of the handle and the faucet don't seem entirely consistent with each other.
 

Makai

Member
On a less facetious note, looking at the teapot again, there seems to be something...off with it.
Like, the positions of the handle and the faucet don't seem entirely consistent with each other.
I think the model is fine but the camera is a fisheye lens. I dunno what to do about it
 

Koren

Member
I think the model is fine but the camera is a fisheye lens. I dunno what to do about it
It's definitively the camera. Seems fine to me (although not something you would see with bare eyes). If you want to correct it, I'd say the only solution is to move the camera farther and reduce its FoV.

My entire life is a lie.... :p

I was sure I had everything correct, I didn't even think about the print statement!

That was dirty... hah. Just means I need to pay more attention to keep the rules in mind at all times. I finally thought I was getting good :(
I think you are... Your arguments are really sound and clear. Again, the print part was a trap, not something you probably would encounter in real code. I know many are treating print this way because... it happens to me too ;)

I wouldn't be surprised that your solution may be correct with some Python 2 implementations, assuming you remove the parenthesis (my Python 2 interpreter gives the same result without the parenthesis, but I'm not sure how print should work as a keyword instead of a function).
 

Zeus7

Member
Hi folks, looking for a bit of help getting my HIVE query to work.

I am wanting to list the top employees who earn the most money.

Code:

Code:
SELECT emp_name, sal FROM employees
rank() over (order by sal desc) WHERE rank < 11;

Error Code:
FAILED: ParseException line 1:49 cannot recognize input near 'rank' '(' ')' in table source


Any help is appreciated, thanks.
 

Koren

Member
Hi folks, looking for a bit of help getting my HIVE query to work.

I am wanting to list the top employees who earn the most money.

Code:

Code:
SELECT emp_name, sal FROM employees
rank() over (order by sal desc) WHERE rank < 11;

Error Code:
FAILED: ParseException line 1:49 cannot recognize input near 'rank' '(' ')' in table source


Any help is appreciated, thanks.

Which SQL server?

Why not
Code:
SELECT emp_name, sal FROM employees ORDER BY sal desc LIMIT 10
or
Code:
SELECT emp_name, sal FROM employees ORDER BY sal desc FETCH FIRST 10 ROWS ONLY
?

(the syntax depends on the SQL server you use)
 

Two Words

Member
I'm learning pySerial and am working on a project that deals with memory mapped IO. Does anybody know any good tutorials on how to learn this stuff that teaches it as if you've never done serial communication programming or memory mapped IO? The closest thing I've done to this is interprocess communication with forks and pipes. This is more about interhardware communication though.
 
I'm learning pySerial and am working on a project that deals with memory mapped IO. Does anybody know any good tutorials on how to learn this stuff that teaches it as if you've never done serial communication programming or memory mapped IO? The closest thing I've done to this is interprocess communication with forks and pipes. This is more about interhardware communication though.

Is there something specific you want to know? Conceptually it's pretty straightforward. A files contents show up in memory as if it's an array and reading from/writing to the memory actually goes through to the underlying file.

Do you want more like a programming api reference for how to do it in Python?
 

Two Words

Member
Is there something specific you want to know? Conceptually it's pretty straightforward. A files contents show up in memory as if it's an array and reading from/writing to the memory actually goes through to the underlying file.

Do you want more like a programming api reference for how to do it in Python?
Yeah, I'm looking for a tutorial that just walks you through it. I've never had to do anything like this before, so I'm looking for something that explains it in order to teach it for the first time. Most things that I'm finding are just explaining how to do serial programming in Python. I need to learn how serial programming works at all.
 

Zoe

Member
Hi folks, looking for a bit of help getting my HIVE query to work.

I am wanting to list the top employees who earn the most money.

Code:

Code:
SELECT emp_name, sal FROM employees
rank() over (order by sal desc) WHERE rank < 11;

Error Code:
FAILED: ParseException line 1:49 cannot recognize input near 'rank' '(' ')' in table source


Any help is appreciated, thanks.

I've never used rank(), but all the examples I'm see have it as one of the columns. Doesn't look like you should be using it there.
 
Hi folks, looking for a bit of help getting my HIVE query to work.

I am wanting to list the top employees who earn the most money.

Code:

Code:
SELECT emp_name, sal FROM employees
rank() over (order by sal desc) WHERE rank < 11;

Error Code:
FAILED: ParseException line 1:49 cannot recognize input near 'rank' '(' ')' in table source


Any help is appreciated, thanks.

Why don't you just use

Code:
SELECT TOP 10 emp_name, sal FROM employees order by sal desc;
 

Erudite

Member
Wondering if anyone who's familiar with Haskell is able to help me.

Given an integer n (> 0), I need to return a list of all possible binary numbers of size n, where the binary numbers are represented as a list of [Bit], where the type Bit is defined as

Code:
data Bit = Zero | One
	deriving (Show, Eq)

So as an example, if the function is given 2, it will return
Code:
[ [Zero,Zero], [Zero,One], [One,Zero], [One, One] ]

I've figured it out how to do it with recursion:

Code:
pad_zeroes :: Int -> [Bit] -> [Bit]
pad_zeroes num list
	| (length list) > num = list
	| (length list) == num = list
	| otherwise = [Zero] ++ (pad_zeroes (num - 1) list)
	
int_to_bit :: Int -> [Bit]
int_to_bit num
	| num == 0 = []
	| mod num 2 == 0 = (int_to_bit (quot num 2)) ++ [Zero]
	| mod num 2 == 1 = (int_to_bit (quot num 2)) ++ [One]

all_bit_seqs :: Int -> [[Bit]]
all_bit_seqs num = (map (pad_zeroes num) (map int_to_bit [0..((2 ^ num) - 1)]))

However, the question is worth 2 marks, and the second mark is awarded for doing it without recursion, and we're only allowed to use functions defined in Prelude.

Any chance someone can point me in the right direction?

Edit: The best I've come up with thinking about it so far is given some decimal value x, I'm able to find out how many bits will be in the binary representation of x by ceiling(x/2). With that, I can initialize a list of Zeros of size ceiling(x/2).

I have a function where I'm able to invert the bits in a list, but I can't figure out what kind of conditional I should put in place to see whether or not the bit needs to be flipped.
 

Jokab

Member
Wondering if anyone who's familiar with Haskell is able to help me.

Given an integer n (> 0), I need to return a list of all possible binary numbers of size n, where the binary numbers are represented as a list of [Bit], where the type Bit is defined as

Have I understood the problem correctly if my interpretation is that you simply need to get all permutations of size n using Zero and One? I realize you can't use permute since it's in Data.List, but just so that I get the problem.
 

Erudite

Member
Have I understood the problem correctly if my interpretation is that you simply need to get all permutations of size n using Zero and One? I realize you can't use permute since it's in Data.List, but just so that I get the problem.
Yep!

Just a few more examples to make it clear:

Code:
> all_bit_seqs 1
[[Zero],[One]]

> all_bit_seqs 3
[[Zero,Zero,Zero],[Zero,Zero,One],[Zero,One,Zero],[Zero,One,One],
 [One,Zero,Zero],[One,Zero,One],[One,One,Zero],[One,One,One]
]

I should also note it is not necessary for the binary numbers to be in ascending order. They can be in any order.
 

Lois_Lane

Member
Guys, has anyone ever created a search log for an array in java? I have to count the amount of times a specific values are searched then print out the result.

EX. You search Brooklyn 5 times and Bronx 2 times before exiting. I would print out -

Brooklyn ( 5 times)
Bronx (2 times)
 
Guys, has anyone ever created a search log for an array in java? I have to count the amount of times a specific values are searched then print out the result.

EX. You search Brooklyn 5 times and Bronx 2 times before exiting. I would print out -

Brooklyn ( 5 times)
Bronx (2 times)

java.util.HashMap<string, int>
 

kadotsu

Banned
If you want to use a slow and obtuse solution you could always create a custom delimiter to concat the number a string was called onto the string itself. You would need to tokenize, read out the integer, increment and write back every time you called a string. You would also need to to a linear search (unsorted array) or binary (sorted array) every time to find the string prefix. That way you can still use arrays.
 

Lois_Lane

Member
Yea I'm trying to not to just write the code out for you. Basically I'm saying "you need to use a hash map". But from there I think you shoudl try to figure it out. Sorry :-/

I'm not asking for you to create the code for me, I'm asking what a hash map has to do with what I asked. I'm in a Comp Sci 101 class and we've only now started using objects and global variables. What I wanted was to be pointed towards a tutorial on dealing with multiple changing arrays or how to build a program that can tell when the same array value pops up multiple times and counts it correctly. Will edit my original post for clarity.

How far are you into object oriented programming?

Not very. I've got the very basics down but we've only just started on working on arrays.

If you want to use a slow and obtuse solution you could always create a custom delimiter to concat the number a string was called onto the string itself. You would need to tokenize, read out the integer, increment and write back every time you called a string. You would also need to to a linear search (unsorted array) or binary (sorted array) every time to find the string prefix. That way you can still use arrays.

I don't understand the first part of this. To Concat a string means that you're attaching it to another string right? If so, then how does attaching its array location to its words help me?
 
A HashMap will allow you to map a key object (such as the String "Brooklyn") to a value, such as an Integer (you can't use int, but it's effectively the same). Every time the string is found in the array, you increment the value in the HashMap, or add it if it's not there already. You get the value at that key, then set the new value to that plus one. At the end, you can iterate through all the entries and get the keys (Strings) and values (Integers).
 
Top Bottom