7
votes
What programming/technical projects have you been working on?
This is a recurring post to discuss programming or other technical projects that we've been working on. Tell us about one of your recent projects, either at work or personal projects. What's interesting about it? Are you having trouble with anything?
I've been working my way through AoC'22 to help me learn Rust (as well as reading the book and doing rustlings), currently on Day 11: Monkey Keep Away! Part 1.
So far, I've built the Monkey struct to hold all the values and I've written parsing functions that transform the input into blocks of 7 lines that are then transformed into a Monkey.
The problem I envision running into next is how to iterate through a collection of mutable Monkeys with references to each other, such that they can pass items between one another. I'm thinking of building a struct MBox that holds a Vec of Monkeys, but we'll see!
I was gonna make a distinct topic about this, but figured that it might do better here first.
I'm working on a Fishing Tournament program for a tourney that my uncle hosts every year. I'm currently in the process of laying out the database structure, and I'm having an issue visualizing how to apply scoring rules to what I have so far. Any suggestions or even links to existing systems would be super helpful.
Basic Schema (without anything for Rule or RuleGroup).
It's very anemic right now, but I'd like to start small and add sugar to it later once I have a better idea of where to go.
First idea was to have some sort of Rule object defined in a table, but the more I think of that, the less I like that idea. Maybe something like a Calculator(Fish, Rule) that takes a fish, and a "Rule" and spits a score out. That makes it stateless and only dependent on input. That still leaves me pretty lost on how to define Rule(s) in the context of the application.
So for an example to work through.
There are tiers of fish, and each tier is worth different amounts of points per inch.
IF a fish is caught with a lure, that's an extra 100 points per inch.
There's also bonus points if fish are over a certain size:
So on so forth for T2 and Trash fish.
Going through this, maybe I have the score defined as the return value from a Rule, when it's given a fish? Multiple pre-defined rules, have a haul filtered and each Catch sent to the appropriate Rule to calculate it's score? But that still seems a little... brittle?
Would it be possible to define score as the return value from a Rule, like you said, but instead of filtering just apply every rule to every fish (or, rather, every catch)? That way you could stick all the rules in a list and just iterate over them, adding together the scores each one generates for a given fish. Something like (in Java, assuming some surrounding support code):
So for your example catch of
you would effectively have
tierRule(catch) + lureRule(catch) + bonusRule(catch)
=100 + 0 + 5
.My brother is going to college for Astrophysics and I'm helping in with a data entry project. I'm using Matplotlib and Jupiter through python, and the end goal is that he can take a list of x variables, run it through a polynomial function and plot out a graph with it. When I try to feed the list of variables into the function, it chokes on the exponents and says that lists are not a valid input, so how can I get this to work?
Can you give a bit more detail? What does the list of x variables look like, and what is the polynomial function? If you have a code snippet that could be great.
It gives me this text,
I'm using Jupyter for this work, and this isn't hard data so much as it is a proof of concept. Ideally it would graph the function and make a table, but baby steps.
You should make the list a numpy array instead of a python list so that you can operate on it with
**
. For example, I think:Should work.
Several things:
One:
This function doesn't return anything. It computes a value, but doesn't make it available outside of the scope of the function.
Try instead
Two:
Assuming x was defined as
x=[0, 1, -1, np.pi, 3*1e8, np.e, -3/2]
What you actually gave to the
f
function is a list of integers, and not an individual integer. So Python try to exponent [0, 1, -1, π, ...] but doesn't know what to do with it.What you probably meant to do is to apply the function on every individual element of the list, and make another list out of those. The usual way to do it is with a list comprehension.
So try this instead:
Thanks for your help. Regarding the second point, I'm a little confused on what you're referencing with el.
el
isn’t referencing anything, it’s binding.y=[f(el) for el in x]
is saying “create an array calledy
by iterating over each element inx
, which we will callel
, and callingf(el)
on it.”So
y
will be equal to[f(x[0]), f(x[1]), …]
.What /u/uwhispersilk said. A list comprehension is like a tiny
for
loop that create a list out of another list, and I had to name the temporary variable somehow. I've chosenel
, short for "element" but I could have choseni
orfoo
orheyEEAAOwonSeveralOscarsAintThatNice
instead. It's as if I wrote:But with a one liner instead.
Also look at /u/gpl answer. It's important to know what your function inputs and outputs. Generally Python's standard library assumes doing operation on a single number, but lot's of time in the numeric computation / scientific fields what you want is to work on vectors (for ease of use/readability, but also for performance reasons).
So you'll likely use numpy, and this library includes both an optimized container to hold a collection of number (the numpy array), and the usual math function that works with those container. For instance you have
math.sin
in the standard library, and it has its counterpart in numpy:numpy.sin
. As per the example, it works on both individual number but also numpy arrays:(
linspace(a,b,n)
create a numpy array of n evenly spaced numbers between a and b)There's also a numpy version of power that works similarly:
I built a little flash card program over an evening to help our kids practice their spelling. But now we might end up just using physical note cards. Anyways, here it is:
https://git.sr.ht/~akkartik/spell-cards.love
Happy to help if someone wants to try using it.