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

01-15-2016 , 10:51 AM
i solved mine.
Programming homework and newbie help thread Quote
01-16-2016 , 09:26 AM
how do I do this...

def home
@image = Image.approved.find(1)
end

This is just shows image with image_id 1 in the home page.

I want to create an array such as for example [1, 21, 34, 38, 39, 50 ,51 ,52, 61, 68]
and then have one of those image_ids placed in the home page daily.
So I want to set my code in advance for example so that each day the next image_id will be called in the home page.
Programming homework and newbie help thread Quote
01-17-2016 , 10:26 PM
Hey guys does anybody know how to do this in R? here is my code from part d:

Spoiler:
# Based on p-values, our intercept and x2 are statistically
# insignificant, as they are greater than 0.05 (from 1.c)
# Will drop our intercept and x2 and run a new
# restricted regression model2.R (R: Restricted)

model2.R <- y ~ 0 + x1

#Running regression on new restricted model
reg.model2.R <- lm(formula = model2.R, data = as1data1)

summary(reg.model2.R)

#Analysis of Variance for test statistic and P-value
anova(reg.model1.UR, reg.model2.R)

#Test-statistic: 1.0903
#p-value: 0.3365

#Our high p-value indicates that we fail to reject the null
# hypothesis that our intercept (beta0) and x2 have joint
# significance. beta0 and x2 are statistically insignificant


I now have to do what I believe is a monte carlo simulation but I don't have a clue how to perform it in R as I have very little programming experience. Any help is much appreciated and here is the question in full.
Spoiler:
Estimate the model chosen in d) for 50 randomly drawn samples of size T=100. Note that you should sample without replacement. For each of the randomly drawn samples, store of the estimates of Beta and its standard error. At the end you will have 50 Beta's and corresponding standard errors. Calculate and plot the cumulative average for both.
Programming homework and newbie help thread Quote
01-18-2016 , 11:05 AM
Given the following pseudocode for the merge procedure of a mergesort algorithm, my goal is as follows:

Rewrite the MERGE procedure so that it does not use sentinels, instead stopping once either array L or R has had all its elements copied back to A and then copying the remainder of the other array back into A.

Here is the original code, using sentinels (guards).

Code:
MERGE(A,p,q,r) 
	n1 = q - p + 1
	n2 = r - q
	let L[1,...,n1+1] and R[1,...,n2+1] be new arrays.
	for (i=1 to n1)
		L[i] = A[p+i-1]
	for (j=1 to n2)
		R[j] = A[q+j]
	L[n1+1] = Infinity
	R[n2+1] = Infinity
	i = 1
	j = 1
	for (k=p to r)
		if(L[i] <= R[j])
			A[k] = L[i]
			i = i+1
		else
			A[k] = R[j]
			j = j+1
And here is my code:

Code:
MERGE(A,p,q,r) 
	n1 = q - p + 1
	n2 = r - q
	let L1[1,...,n1] and R[1,...,n2] be new arrays.
	for(i=1 to n1)
		L[i] = A[p+i-1]
	for(j=1 to n2)
		R[j] = A[q+j]
	i=1
	j=1
	for (k=p to r)
		while(i<=n1 AND j<=n2)
			if(L[i] <= R[j])
				A[k] = L[i]
				i = i+1
			else
				A[k] = R[j]
				j = j+1
		if(i>n1)
			A[k] = R[j]
			j = j+1
		else
			A[k] = L[i]
			i = i+1
Does this look good to you guys? Any ways to make this pseudocode smarter/smaller/faster/cleaner? As you can probably tell the condition of the while loop essentially checks if either array L or R has had all it's elements copied, and if so then I move down to the last if/else statement which finishes copying the remaining elements of the other array. Can you guys see anything wrong with this code? Do you think this is an incorrect way to do this based on the wording of the assignment which says to stop once either array L or R has had all its elements copied back to A and then copy the remainder of the other array back into A, meaning I should exit the MERGE function and then copy the rest of the elements by some other copy function?
Programming homework and newbie help thread Quote
01-22-2016 , 03:03 PM
I am using carrier wave to do multiple uploads but they are being saved as an array in a folder and not being issued individual ids.

Do how do I call them in a view? I see that

@photos do |photo|

photo.picture.first

returns the first picture in each array folder but how to I display all the pictures?

photo.picture.all does not work (fml)

Also is there an uploaded that will issue individual ids to photos so that they can be called with the standard params :id ?
Programming homework and newbie help thread Quote
01-25-2016 , 03:05 PM
brain has turned to sludge, can't figure out how to do this

I have two classes that are related in rails, say Bucket and Water

A bucket has_many :water, and water belongs_to :bucket

The bucket has a name and item number. The water has the item number of the bucket it belongs to.

When I'm putting out the info on water in, say, an @water.each loop, how do I get ruby to output the name of the bucket the water belongs to?

thinking about it from a sql perspective, i would think it'd be something like '@bucket.name where bucket.item_number == water.item_number' but I can't think that would work

edit

so I tried this in the waters view:
Code:
@waters.each do |water|
  @buckets.each { |bucket| bucket.name if bucket.item_number == water.item_number }
end
but it complained that 'undefined method `each' for nil:NilClass' (referring to the buckets line). Tried to add buckets as a resource for the water resource in routes, but that didn't help =-/

Last edited by Loki; 01-25-2016 at 03:31 PM.
Programming homework and newbie help thread Quote
01-25-2016 , 05:04 PM
LK,

Isn't it just something like this?

Code:
 @waters.each { |water| water.bucket.name }
Programming homework and newbie help thread Quote
01-25-2016 , 08:52 PM
well, water belongs_to a bucket, it doesn't have a bucket reference in it that I can access like that.

I get the same "undefined method `name' for nil:NilClass" error when I try it that way. I feel like I need the water views to get a reference to the @buckets, but i'm not sure

edit

I think I'm getting somewhere.

I went into the water controller and added a reference to bucket.all whenever the index was loaded.

It's hideous and not what I want, but it's not an error message! So, step in the right direction.

Last edited by Loki; 01-25-2016 at 09:02 PM.
Programming homework and newbie help thread Quote
01-25-2016 , 09:14 PM
I know nothing about Ruby or Rails but this seems like a problem:

Quote:
Originally Posted by Noodle Wazlib
well, water belongs_to a bucket, it doesn't have a bucket reference in it that I can access like that.
But looking at it in basic terms, accessing the corresponding bucket from water objects is precisely what you want to do. Therefore, put reference to bucket in the water objects and hook it up to the correct instance of bucket when you load the water objects.
Programming homework and newbie help thread Quote
01-25-2016 , 09:26 PM
i wasn't terribly far off with my original code.

I got it working by having that view pass a reference to Bucket.all, then something like
buck = ''
@buckets.each do { |bucket| buck = bucket.Bucket_Name if bucket.item_number == water.item_number }

seems like i'd have to keep passing the @buckets variable into all the water views if i want to use them though, and that seems suboptimal. Definitely need to figure out a good way to use the relational model stuff or something. Maybe that is the only way.

Or, as you're saying, if I create buckets first, and then create water that goes in those buckets and give them references to the bucket they go in upon creation, problem solved, likely
Programming homework and newbie help thread Quote
02-02-2016 , 01:07 PM
question regarding 'pythonic'

i had this for a solution:

Code:
def censor(text, word):
    size = len(word)
    replaced = text.replace(word.lower(), '*'*size)
    return replaced
and changed it to this after realizing i was doing some tedious assignment statements:

Code:
def censor(text, word):
    return text.replace(word.lower(), '*'*len(word))
but i'm curious, they had mentioned something like 80 characters being the max length for a line to be in line with current python trends. Is that idea considered 'pythonic' or what the heck does that mean?

Best way to find current 'pythonic' standards?
Programming homework and newbie help thread Quote
02-02-2016 , 02:41 PM
80 characters is a pretty standard line length for code that goes back to the number of columns on punch cards as well as the number of characters that could be displayed on a single line with early terminal displays
Programming homework and newbie help thread Quote
02-02-2016 , 05:08 PM
Quote:
Originally Posted by Noodle Wazlib
question regarding 'pythonic'

i had this for a solution:

Code:
def censor(text, word):
    size = len(word)
    replaced = text.replace(word.lower(), '*'*size)
    return replaced
and changed it to this after realizing i was doing some tedious assignment statements:

Code:
def censor(text, word):
    return text.replace(word.lower(), '*'*len(word))
but i'm curious, they had mentioned something like 80 characters being the max length for a line to be in line with current python trends. Is that idea considered 'pythonic' or what the heck does that mean?

Best way to find current 'pythonic' standards?
For starters, that line is only len(52), so it is below 80 characters.

Second, there is nothing about pythonic or any *ic that, IMO, resolves to a one-liner. In fact, I'd argue that one-liners are often a terrible thing. One-liners can be hard to read, and if you are trying for "functional," you are often creating programs that are much much slower.

Case in point: http://stackoverflow.com/questions/1...-comprehension

Pythonic tends to mean something different. A simple example is:

Code:
my_word = "one"

# not pythonic:
for i in range(len(my_word)):
    print(my_word[i])

# pythonic:
for i in my_word:
    print(i)
I tend to hold onto the 80-character limit. It forces me to consider what my code is doing. Like anything, constraints force something good more often than not.

The only thing I find hard to read about your code, both versions, is not using a space between the multiplier and the string. Also, not sure why you need the lower if you are replacing the string with "****" anyways.

Last edited by daveT; 02-02-2016 at 05:13 PM.
Programming homework and newbie help thread Quote
02-02-2016 , 06:09 PM
'Replace' and 'replace' aren't equal, I assume

And i haven't had a lot of time to play around with whitespace so far. Not sure when you have to have something right next to another thing and when you can make it more readable.

Thanks for the tips. I eventually looked for info on 'pythonic' and came across something similar, mostly stating it's about using conventions that have evolved with the language.
Programming homework and newbie help thread Quote
02-02-2016 , 09:36 PM
This advice is as good as any

Quote:
Always surround these binary operators with a single space on either side: assignment ( = ), augmented assignment ( += , -= etc.), comparisons ( == , < , > , != , <> , <= , >= , in , not in , is , is not ), Booleans ( and , or , not ).
https://www.python.org/dev/peps/pep-...and-statements
Programming homework and newbie help thread Quote
02-02-2016 , 11:35 PM
i'm still not 100% sure if I can put a space between function names and parameter lists >.<
Programming homework and newbie help thread Quote
02-03-2016 , 07:20 AM
Quote:
Originally Posted by daveT
For starters, that line is only len(52), so it is below 80 characters.

Second, there is nothing about pythonic or any *ic that, IMO, resolves to a one-liner. In fact, I'd argue that one-liners are often a terrible thing. One-liners can be hard to read, and if you are trying for "functional," you are often creating programs that are much much slower.

Case in point: http://stackoverflow.com/questions/1...-comprehension

Pythonic tends to mean something different. A simple example is:

Code:
my_word = "one"

# not pythonic:
for i in range(len(my_word)):
    print(my_word[i])

# pythonic:
for i in my_word:
    print(i)
I tend to hold onto the 80-character limit. It forces me to consider what my code is doing. Like anything, constraints force something good more often than not.

The only thing I find hard to read about your code, both versions, is not using a space between the multiplier and the string. Also, not sure why you need the lower if you are replacing the string with "****" anyways.
I share your view on the bolded especially. Nice post.
Programming homework and newbie help thread Quote
02-04-2016 , 03:59 PM
Quote:
Originally Posted by gaming_mouse
One method I use is to pretend that the boundary conditions don't matter. Just get each cell's neighbors, all of them, including the negative index ones and the too large ones. Then just run the all through "filter()" call, where the filter function encapsulates the logic for invalid cells (ie, negative or too big).
This came up again and this method was great, just tagged a small filter onto the end of the array.

But I know that php will give you errors when you try to access out of bound indices. I think C just returns junk data but java might have out of bounds errors too... In one one of these types of languages would you consider just catching the out of bounds errors and ignoring them or use a different approach?
Programming homework and newbie help thread Quote
02-04-2016 , 05:07 PM
You should use a different approach and avoid referring to invalid cell indices. In statically typed languages like Java and C#, throwing and catching exceptions as a method for controlling flow of execution is considered bad practice. Reasons are:

Semantic - an exception, even if caught and handled, is supposed to indicate some actual problem preventing normal execution.

Performance - throwing exceptions in these languages involves a fair bit of overhead.

In duck-typed or other dynamic languages, using errors as a way to control execution flow is totally acceptable. The Python community even has an acronym EAFP - "easier to ask for forgiveness than permission", meaning the "pythonic" way to code is often to attempt to do what you want to do and handle errors if it doesn't work.
Programming homework and newbie help thread Quote
02-04-2016 , 05:18 PM
Wouldn't be using exceptions for control flow, just discarding them and continuing on because the invalid values would be filtered out after collection.
Programming homework and newbie help thread Quote
02-06-2016 , 06:46 PM
Quote:
Originally Posted by fruit snacks
This came up again and this method was great, just tagged a small filter onto the end of the array.

But I know that php will give you errors when you try to access out of bound indices. I think C just returns junk data but java might have out of bounds errors too... In one one of these types of languages would you consider just catching the out of bounds errors and ignoring them or use a different approach?
I think the filtering approach works everywhere. It's just that it's not going to be as concise in C/Java. But that's simply true in general with those languages.
Programming homework and newbie help thread Quote
02-07-2016 , 08:46 AM
Quote:
Originally Posted by fruit snacks
This came up again and this method was great, just tagged a small filter onto the end of the array.

But I know that php will give you errors when you try to access out of bound indices. I think C just returns junk data but java might have out of bounds errors too... In one one of these types of languages would you consider just catching the out of bounds errors and ignoring them or use a different approach?
In C basically it shows up at run time. If it happens at the user level the OS will terminate the program with an error condition and basically you will be left to your own devices to figure out where the problem is but you will be able to at least hopefully have a better repetitive scenario. If you are programming at the kernel mode level or any environment where you don't have virtual memory management protection you will have a much more difficult time finding the problem. This is one huge drawback to the monolithic kernel mode model vs. the micro kernel mode model. I can tell you from experience that Microsoft has invested a lot of time and money in tools and support software to weed these types of issues out of Windows code. Linux has the nice advantage of having an incredible number of eyeballs looking at the code. In bare metal type programming companies spend a lot of time and money trying to keep those types of errors out of the software they deliver.
Programming homework and newbie help thread Quote
02-07-2016 , 04:17 PM
valgrind can be extremely helpful for tracking down programmer ****ups like that in C/C++. It can detect:
- reading/writing off the end of arrays (more generally, writing/reading to/from memory your program hasn't properly allocated)
- reading the contents of uninitialized variables
- memory leaks
Programming homework and newbie help thread Quote
02-07-2016 , 06:08 PM
I thought because arrays in c are essentially just pointer arithmetic, it would just read in whatever is in memory at that location and cast it to an integer.

Also for the languages with out of bounds errors, say we're getting all sibling cells so toward the end of the array we'll be hitting array indices larger than the array size, throwing an error.

Valgrind seems cool, but here we're intentionally getting out of bounds values knowing we're going to filter them out later.
Programming homework and newbie help thread Quote
02-08-2016 , 08:16 AM
Quote:
Originally Posted by goofyballer
valgrind can be extremely helpful for tracking down programmer ****ups like that in C/C++. It can detect:
- reading/writing off the end of arrays (more generally, writing/reading to/from memory your program hasn't properly allocated)
- reading the contents of uninitialized variables
- memory leaks
Also helpful is using a non prehistoric language that accomplishes these things natively.
Programming homework and newbie help thread Quote

      
m