untonuggan: four different colored panels of the MRI image of a brain (brain)
[personal profile] untonuggan
I am playing with code to learn Python moar, because I really learn better by using stuff than by trying to memorize abstract things, and even moar when it feels useful.

So yes.

It is going well except I'm trying to do a Thing which feels like it should feel simple except maybe I don't know the search term for what it's called or maybe in actual programming it's not simple? So I thought I'd ask the hive mind here and then if no luck maybe move on to Python forums hivemind or something.

Basically I am making a program that automates making a hat recipe, because I know a lot of math-phobic people who don't make up their own hat patterns because math.

I have a lot of the input features programmed and if/then boolean type stuff.

My conundrum is, I am looking for some sort of thing that goes something like:

here are several possible number of stitches to cast on; they work with a given pattern repeat, gauge, and head size (that coding is already done)
here are several integers (7, 8. 9, 10, 11, 12, let's say)
which of those integers go into the stitch totals with a remainder of zero?
give me those integers-cast on combos, that will be your choice of decreases-cast on pattern combos
(then I can insert it into a pre-scripted pattern text which is on Ravelry or something)

I've been looking at sets and conditional statements and lists and I started to look at regular expressions but by then my brain had hit "no more concentration for you tonight, also you keep snapping at people who interrupt your train of thought." so yup.

any feedback appreciated!

Date: 2016-01-22 10:57 pm (UTC)
yvi: Kaylee half-smiling, looking very pretty (Default)
From: [personal profile] yvi
I am almost asleep right now, so I may be misunderstanding where the disconnect is, but maybe you are just missing the term "modulo"? That's the function where you get the remainder of a division.

Date: 2016-01-22 10:59 pm (UTC)
yvi: Kaylee half-smiling, looking very pretty (Default)
From: [personal profile] yvi
And that's % in Python, if I remember correctly. It's used quite often by programmers and almost never by anyone else ;-)

Date: 2016-01-22 11:00 pm (UTC)
randomling: A wombat. (Default)
From: [personal profile] randomling
I think what you want there is the modulo operator, which gives the remainder of a division. It's % (that's percent sign, in case of encoding difficulties) in most languages, including Python (pretty sure).

So 4 % 2, for example, will return a result of 0, because 4 / 2 has a remainder of 0.

Thus you'll want something that looks like (in terrible psuedocode, because it's ages since I did any Python):

for int in range(7,12):
if int % stitch-total == 0:
[ whatever you want to do with it here ]

I can't remember exactly how Python does for-loops, so like I said, horrible psuedocode...

Let me know if that wasn't clear enough? I have a bit of brain fog going on, sorry.

Date: 2016-01-23 04:12 am (UTC)
jadelennox: Grey's Anatomy: Cristina smiling (grey's: cristina smiling)
From: [personal profile] jadelennox
What those smart peoples said re: modulo, but also, if other humans are inspiring at all, I think there's Pyladies near you: http://www.pyladies.com/ The pyladies in my area run weekly beginner project afternoons and are ridiculously supportive and kind.

Date: 2016-01-23 02:55 pm (UTC)
silveradept: A kodama with a trombone. The trombone is playing music, even though it is held in a rest position (Default)
From: [personal profile] silveradept
I'll agree with above regarding the modulo operator - it's specifically for calculating remainders of division operations. So, if your set of integers is constant, no matter what options are selected, you can set them as an array (or possibly as a tuple - I still don't fully understand Pythonic data structures), ALLCAPS their name to indicate them as a constant, then feed the stitch totals in using a for loop, as above, do the modulo operation against the constants (stitch_total % integer), and only keep those items in the stitch_total array that produce a zero in the modulo.

I would probably code it as two loops, but there's probably a way to smash it together into one statement.

for total in stitch_total:
for integer in MODULO_INTEGERS:
if total % integer == 0:
working_combinations.push(total)
end
end
end

Profile

untonuggan: Lily and Chance squished in a cat pile-up on top of a cat tree (buff tabby, black cat with red collar) (Default)
lizcommotion

October 2017

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 29th, 2025 02:54 pm
Powered by Dreamwidth Studios
OSZAR »