Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

09-27-2016 , 12:40 PM
It's something I'm very familiar with because I often deal with checksum algorithms, which utilize various-sized XOR sums.
Programming homework and newbie help thread Quote
09-27-2016 , 03:03 PM
hate to be that guy, and asking in full respect, but what is this for?
Programming homework and newbie help thread Quote
09-27-2016 , 10:36 PM
Quote:
Originally Posted by ChrisV
One of the bolded assuming you're running Windows or OSX.
ugh I'm so bad at this. I downloaded the zip file, realized it was (both downloaded and unzipped) in the wrong folder, moved it to the R library, still getting the same error.

i thought it was odd that there's an Rcpp 0.12.7 folder with nothing but another Rcpp, is it something to do with the way i moved the file or unzipped it?

LOL i am trying to post the image of what i mean but now 2p2 is censoring the link to that as well, wtf. i can't for the life of me figure out what it's trying to censor but below is the URL minus the spaces (just showing the Rcpp within the Rcpp 12.7, probably not even helpful, idk).

https://posti mg.or g/image/m69znv5dh/
Programming homework and newbie help thread Quote
09-29-2016 , 01:48 AM
Say language L = a^n b^n c^n, n >= 0. So strings in that language would be the empty string, abc, aabbcc, aaabbbccc, etc. I'm trying to make an underlying CFG for this (that I will then use attribute grammar on). I'm stuck on the context free grammar part.
Programming homework and newbie help thread Quote
09-29-2016 , 01:58 AM
Welp, I think this should work?

Ax --> a Ax | empty
Bx --> a Bx | empty
Cx --> c Cx | empty

then if I have AxBxCx I can make empty from it. abc from it, aabbcc from it, aaabbbccc from it. Only problem is I can also make abbc from it which shouldn't be part of the language. But this should work for my staring CFG?
Programming homework and newbie help thread Quote
09-29-2016 , 10:33 AM
Quote:
Originally Posted by Ryanb9
Welp, I think this should work?

Ax --> a Ax | empty
Bx --> a Bx | empty
Cx --> c Cx | empty

then if I have AxBxCx I can make empty from it. abc from it, aabbcc from it, aaabbbccc from it. Only problem is I can also make abbc from it which shouldn't be part of the language. But this should work for my staring CFG?
It seems to me you're making proxies for the fundamental elements of your language which I don't think you're required to do to define your cfg.

What I think makes more sense is for you to express exponentiation in your language and then express a word as exponentiation of your fundamental characters a,b,c or an empty string.

Either that or define concatenation in some way but make it repeatable on single language elements (so that you could concatenate a with as many A's as you need but then also concatenate that with b's etc).
Programming homework and newbie help thread Quote
09-29-2016 , 05:08 PM
Quote:
Originally Posted by daveT
hate to be that guy, and asking in full respect, but what is this for?
What's a Hamming Code, you mean?

It's way for the recipient of a data transmission to detect and correct errors on a fairly simple level. It can only correct one-bit errors, but it's a step ahead of things like parity checks that can detect, but not correct.
Programming homework and newbie help thread Quote
10-03-2016 , 08:14 PM
Given the following schema:

Code:
CREATE TABLE Classes (
  class varchar(20),
  type varchar(2),
  country varchar(20),
  numGuns int,
  bore int,
  displacement int
);
CREATE TABLE Battles (
  name varchar(20),
  date DATE
);
CREATE TABLE Outcomes (
  ship varchar(20),
  battle varchar(20),
  result varchar(20)
, sunkBy varchar(20));
CREATE TABLE Ships (
  name varchar(20),
  class varchar(20),
  launched int
);
I'm trying to for each class of ships that includes at least 2 ships, find the number of ships sunk by a ship of that class.

I have the following sql query:

Code:
WITH M AS(
  SELECT class from Ships
  GROUP BY class
  HAVING COUNT(class) > 1
)
SELECT ships.class, COUNT(*)
FROM Outcomes, Ships, m 
WHERE Outcomes.sunkby = Ships.name AND M.class = Ships.class
GROUP BY ships.class;
But it only returns info about classes who have sunk at least 1 ship. The where clause causes classes with no sunken ships to be ignored.
I would however like to list also the classes who haven't sunk any ships, and have them displayed as {'ClassName', 0}.

To abstract the question a bit, I'm looking for a way to return also rows that have count = 0, in a count/group by query.
Programming homework and newbie help thread Quote
10-03-2016 , 08:51 PM
I would approach this from another direction.

Code:
with x as (
   select ships.name, count(*) as c
   from ships, outcomes
   where ships.name = outcomes.sunkby
  group by ships.name
)
select ships.class, sum(x.c) as sunk, count(*) as class_size
from ships
left join x on (ships.name = x.name)
group by ships.class
having class_size > 1
I assume you are allowed to use outer joins. Left join here means "include all rows in the first table, with nulls for fields in the 2nd table that don't have a match". This lets us make sure that every ship class is represented.

Note that sum(x.c) will, for most databases, be null if there are no qualifying ships in that class. Each database handles this in their own way. Oracle uses NVL, sqlite uses IFNULL, etc. You'd have to look up your particular databases' method.
Programming homework and newbie help thread Quote
10-07-2016 , 11:22 PM
Trying to do a CFG that will accept things like "x = 1.5 + 2;" "y = x * (-3)" "# This is a comment"

Code:
stmt    : assn ';'
        | com
assn    : id '=' expr
expr    : id
        | numb
        | expr opr expr
        | '(' expr ')'
        | '(' '-' expr ')'
numb    : int
        | int '.' int
com     : '#' (string | epsilon)
So you read the above like this: "statement is either an assignment followed by a semicolon or a comment" i guess

int is 0-9, string is any combo of letters, epsilon is empty

Will this work? Sometimes there's some weird recursion spots that will end up accepting things I dont want to accept but I've looked at this for a while and it seems okay, although the (-expr) and (expr) seems a bit redundant like maybe I could solve it with less code in the language but I want to enforce that any negative assignment be inside a parenthesis.
Programming homework and newbie help thread Quote
10-08-2016 , 03:30 AM
Quote:
Originally Posted by Mavoor
Given the following schema:

I would however like to list also the classes who haven't sunk any ships, and have them displayed as {'ClassName', 0}.
Rusty is right about using left join, but to convert null to 0 you have two options, coalesce and case.

You didn't say what RDMS you are using, so I'll default to giving you the Postgres version of each, though I think both structures are available in pretty much all RDMS's.

coalesce() takes to arguments, evaluating the first argument that is not null.

first arg is not null:
Code:
select coalesce(1, 2);
>> 1
first arg is null:
Code:
select coalesce(null, 2);
>> 2
In your case, you want coalesce(sum(x.c), 0), which will convert all your nulls to 0.

You may also do this with CASE, which looks like this:

Code:
select
case
   when x.c is null 
	then 0
   else x.c
end
from ....
Programming homework and newbie help thread Quote
10-08-2016 , 08:29 AM
Quote:
Originally Posted by Ryanb9
Trying to do a CFG that will accept things like "x = 1.5 + 2;" "y = x * (-3)" "# This is a comment"

Code:
stmt    : assn ';'
        | com
assn    : id '=' expr
expr    : id
        | numb
        | expr opr expr
        | '(' expr ')'
        | '(' '-' expr ')'
numb    : int
        | int '.' int
com     : '#' (string | epsilon)
So you read the above like this: "statement is either an assignment followed by a semicolon or a comment" i guess

int is 0-9, string is any combo of letters, epsilon is empty

Will this work? Sometimes there's some weird recursion spots that will end up accepting things I dont want to accept but I've looked at this for a while and it seems okay, although the (-expr) and (expr) seems a bit redundant like maybe I could solve it with less code in the language but I want to enforce that any negative assignment be inside a parenthesis.
I could be wrong but I was under the impression you had to define things first before you used them to define other things. For example, I thought you had to define assgn and com before their used to define statement.

I'm also fairly certain order might matter here if you actually have to define how addition, multiplication, etc work as there is an order of operations to take into account.
Programming homework and newbie help thread Quote
10-08-2016 , 11:50 AM
What language?

In python (which this clearly is not) the resultset has a rownumber attribute. But I probably wouldn't use it, I'd probably just use "enumerate", and for whatever langauge you're in you could just keep a counter.
Programming homework and newbie help thread Quote
10-08-2016 , 11:52 AM
Ohh you replied so quickly I deleted my post in the meantime and wanted to do it new. It was Java.
Programming homework and newbie help thread Quote
10-08-2016 , 11:57 AM
Code:
       ResultSet rs = stmt.executeQuery( "SELECT XXX FROM XYZ;" );
	         while ( rs.next() ) 
	         {	        
	         String test = rs.getString("XXX");
	         }
The goal was to have a choicebox in Javafx have all the strings from the database as choices. I wanted to have the method return the "test" string, but then it would obviously quit with the first result. So next try was to have a pointer to rs and recursively call the method but that didn't work. So I guess I have to have a rowcounter and then just return the element in row x.

I guess the easiest way would probably be to count the rows with one method, then with another method return the current row and recursively call that method with row +1.
Programming homework and newbie help thread Quote
10-08-2016 , 12:02 PM
Any reason not to create an array of strings and return that? Or to create an interator that returns results one at a time? Either of these is very simple in python, but I don't know anything about java.
Programming homework and newbie help thread Quote
10-08-2016 , 12:05 PM
I did solve it with an arraylist, but that seemed very inefficent when I got a Database with the data anyways to then save it in an arraylist. It's the first time I worked with Databases and Javafx and was pretty much experimenting.
Programming homework and newbie help thread Quote
10-08-2016 , 02:11 PM
Quote:
Originally Posted by RustyBrooks
What language?

In python (which this clearly is not) the resultset has a rownumber attribute. But I probably wouldn't use it, I'd probably just use "enumerate", and for whatever langauge you're in you could just keep a counter.
No language, its just a context free grammar. oh, nvm you weren't replying to me
Programming homework and newbie help thread Quote
10-13-2016 , 12:45 AM
Quote:
Originally Posted by Ryanb9
Say language L = a^n b^n c^n, n >= 0. So strings in that language would be the empty string, abc, aabbcc, aaabbbccc, etc. I'm trying to make an underlying CFG for this (that I will then use attribute grammar on). I'm stuck on the context free grammar part.
L = { a^n b^n c^n | n >= 0 } isn't a context free language, which you can proof via the pumping lemma for CFL's. It's however easily recognizable by a Turing machine, and probably one of the first examples of a language that isn't context free but can be recognized by a Turing machine.


Quote:
Originally Posted by Ryanb9
Trying to do a CFG that will accept things like "x = 1.5 + 2;" "y = x * (-3)" "# This is a comment"

Code:
stmt    : assn ';'
        | com
assn    : id '=' expr
expr    : id
        | numb
        | expr opr expr
        | '(' expr ')'
        | '(' '-' expr ')'
numb    : int
        | int '.' int
com     : '#' (string | epsilon)
So you read the above like this: "statement is either an assignment followed by a semicolon or a comment" i guess

int is 0-9, string is any combo of letters, epsilon is empty

Will this work? Sometimes there's some weird recursion spots that will end up accepting things I dont want to accept but I've looked at this for a while and it seems okay, although the (-expr) and (expr) seems a bit redundant like maybe I could solve it with less code in the language but I want to enforce that any negative assignment be inside a parenthesis.
Could you give an example of where this cfg accepts something that it shouldn't accept? I think it looks good at first glance but not sure.
Btw, for completeness I think you should list out all the terminals, that is end symbols. ie create a new rule int -> 0 | 1 | ... | 9, and do something similar for strings (and include epsilon as a string instead of defining it as its own identity)
Programming homework and newbie help thread Quote
10-24-2016 , 04:32 AM
Is there no publically available AHK code for scanning cards on Stars? When I google it, the only hit I get is for writing a bot which only explains the concept but no code.
Programming homework and newbie help thread Quote
10-24-2016 , 09:20 AM
Given the following DB schema:

Code:
Human(Id, gender, ...)
Man(Id, ...)
Woman(Id, ...)
Where Id is an unique number within the Man/Woman tables, but not unique within Human (Because a man and a Woman might have the same Id).
{Id, gender} is however Unique within Human because no human can have the same Id as another human of the same gender.
How can I create a relationship between Man/Woman and Human. There is a one-to-one relationship between them, but how can I reference the correct tuple within Human, given information from Man. The Id alone is not enough because I might get information about a woman.
Do I need to store gender information within the Man/Woman tables as well? Or is there another way of doing this?
Programming homework and newbie help thread Quote
10-24-2016 , 12:57 PM
Quote:
Originally Posted by Mavoor
Given the following DB schema:

Code:
Human(Id, gender, ...)
Man(Id, ...)
Woman(Id, ...)
Where Id is an unique number within the Man/Woman tables, but not unique within Human (Because a man and a Woman might have the same Id).
{Id, gender} is however Unique within Human because no human can have the same Id as another human of the same gender.
How can I create a relationship between Man/Woman and Human. There is a one-to-one relationship between them, but how can I reference the correct tuple within Human, given information from Man. The Id alone is not enough because I might get information about a woman.
Do I need to store gender information within the Man/Woman tables as well? Or is there another way of doing this?
If you have control of the schema then Id should be the unique primary key in each table and then you should have a foreign key in the human' table that matches the primary key (id) in the other tables.

Sent from my SM-G900R4 using Tapatalk
Programming homework and newbie help thread Quote
10-24-2016 , 01:29 PM
How do you create a foreign key that knows which table it should look up against?
An Id can match both tables. The Human table has enough info to know which table it should look up against (gender), but is it possible to define this as part of a db schema?
Programming homework and newbie help thread Quote
10-24-2016 , 02:38 PM
Quote:
Originally Posted by Mavoor
How do you create a foreign key that knows which table it should look up against?
An Id can match both tables. The Human table has enough info to know which table it should look up against (gender), but is it possible to define this as part of a db schema?
I maybe using the term schema wrong here relative SQL newbie.

I always thought what I described was standard relational data modeling practice. You don't want tables containing duplicate information, especially if it's going to be used to identify the row later (i.e. ids should not be repeated in humans, nor in any other table).

You just define a new column in humans as a foreign key and make sure it matches the Id from the other tables. I think it's relatively easy but not something I could just rattle off because like I said relative newbie and haven't built anything using SQL yet. Pretty small language though and I imagine it wouldn't take long to find it online. I'm sure any number of posters here could tell you outright from memory.
Programming homework and newbie help thread Quote
10-24-2016 , 02:57 PM
Quote:
Originally Posted by just_grindin
I always thought what I described was standard relational data modeling practice. You don't want tables containing duplicate information, especially if it's going to be used to identify the row later (i.e. ids should not be repeated in humans, nor in any other table).
Just to be a little more clear here I meant that each id within a table should be unique. So no id in each table should be the same and then that would be your primary key to identify a unique dataset in the table.

This then holds true for all tables, but not necessarily across tables, so that the dataset belonging to id 1 in the Humans table can be the same as or different than the dataset belonging to id 1 in the other tables. The foreign key in the humans table is identical to the primary key in the other tables.

You can also insert the primary key of the humans table as foreign keys in the other tables. That way you can query any table and know exactly which record you're looking for.

I'd also be curious why you have 2 separate tables for genders.
Programming homework and newbie help thread Quote

      
m