I am trying to get the margin of two items in an array, the array consists of 4 float values each, and I want the first array item to be subtracted by the first array item in the second array, and so on for the rest of the array.  My current code looks like this:  buyPrice = [     '456.3',     '2346.5',     '123.5',     '43.5', ]  sellPrice = [     '426.3',     '1346.5',     '23.5',     '13.5', ]  total = 0 amount = 0  userInput = int(input("How much coins do you want to spend?: "))  for x in buyPrice:     while total &lt;= userInput:         total += float(x)         amount += 1      for y in sellPrice:         k = float(x) - float(y)       print("Profit: " + str(k))     print("Total Cost: " + str("{:.1f}".format(total)))     print("Buy: " + str(amount))     print("----------")     amount = 0     total = 0     k = 0   And the MARGIN output is this:  Item 1: -382.8 Item 2: -1303,0 Item 3: 20 Item 4: 30.0   Output should be:  Item 1: 30 Item 2: 1000 Item 3: 100 Item 4: 30   I think my other code is interrupting this, as I am trying to get: 1. Total amount of items I can buy with the user Input (product price: 10k - userInput = 100k - total amount of items that can be bought: 10 - and so on) 2. The profit the user can make if they are selling the item for a higher price. 3. The total cost of buying all the products (total = buyprice + buyprice)  I could just be completely of right now, it is early in the morning, but I am completely lost.   EDIT: After some debugging on this I see that "X" never changed value, it kept being "426.3", most likely because of my for loop? How can I fix that?  Thanks
10585
Right now I have vector3 values represented as lists. is there a way to subtract 2 of these like vector3 values, like  [2,2,2] - [1,1,1] = [1,1,1]   Should I use tuples?  If none of them defines these operands on these types, can I define it instead?  If not, should I create a new vector3 class?
117819
I am using KOMA script for my document.  Occasionally, I have a float that is wider than text width, and I want the float to be pushed into the outer margin.  This is why I defined \widefloat command, that checks if page is odd using command \ifthispageodd and depending on the results pushes the float left or right.  This works fine when float is in the middle of the page.  However, when float is thrown into the next page, \ifthispageodd gives wrong result.  Could you please tell me how to fix this up?  MWE: the first float is correctly pushed into outer margin, the second is pushed into inner margin.  \documentclass[11pt]{scrbook} \usepackage{tikz,lipsum}  \newcommand{\widefloat}[1]{% \ifthispageodd{\makebox[\textwidth][l]{#1}}{\makebox[\textwidth][r]{#1}}}  \begin{document}  \lipsum[1]  \widefloat{ \begin{tikzpicture} \draw (0,0) rectangle (16cm,10cm); \end{tikzpicture}}  \lipsum[3]  \widefloat{ \begin{tikzpicture} \draw (0,0) rectangle (16cm,10cm); \end{tikzpicture}}  \lipsum[2]  \end{document}
106
I need a good explanation (references are a plus) on Python's slice notation.   To me, this notation needs a bit of picking up.   It looks extremely powerful, but I haven't quite got my head around it.
85430
Here's a silly fun question:  Let's say we have to perform a simple operation where we need half of the value of a variable. There are typically two ways of doing this:  y = x / 2.0; // or... y = x * 0.5;   Assuming we're using the standard operators provided with the language, which one has better performance?  I'm guessing multiplication is typically better so I try to stick to that when I code, but I would like to confirm this.   Although personally I'm interested in the answer for Python 2.4-2.5, feel free to also post an answer for other languages! And if you'd like, feel free to post other fancier ways (like using bitwise shift operators) as well.
93651
I want to compare 2 arrays of objects in JavaScript code.  The objects have 8 total properties, but each object will not have a value for each, and the arrays are never going to be any larger than 8 items each, so maybe the brute force method of traversing each and then looking at the values of the 8 properties is the easiest way to do what I want to do, but before implementing, I wanted to see if anyone had a more elegant solution. Any thoughts?
2695
Some of you may recognize this as Project Euler's problem number 11. The one with the grid.  I'm trying to replicate the grid in a large multidimensional array, But it's giving me a syntax error and i'm not sure why  grid = [  [ 08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08 ], [ 49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00 ], [ 81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65 ], ...   And I get this error:     File "D:\development\Python\ProjectEuler\p11.py", line 3     [ 08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91 , 08 ],        ^ SyntaxError: invalid token   Why is it throwing an error before the comma?
27093
I occasionally see the list slice syntax used in Python code like this:  newList = oldList[:]   Surely this is just the same as:  newList = oldList   Or am I missing something?
15307
Given $[a_{ij}]_{10\times10}=\left[ \begin{array}{ccc}   1&amp;2&amp;4&amp;7&amp;...&amp;...&amp;...\\   3&amp;5&amp;8&amp;...&amp;...&amp;...&amp;...\\   6&amp;9&amp;...&amp;...&amp;...&amp;...&amp;...\\   10&amp;...&amp;...&amp;...&amp;...&amp;...&amp;...&amp;\\   ...&amp;...&amp;...&amp;...&amp;...&amp;...&amp;...\\   ...&amp;...&amp;...&amp;...&amp;...&amp;...&amp;98\\   ...&amp;...&amp;...&amp;...&amp;...&amp;99&amp;100\\ \end{array} \right]$, what's $a_{7,3}+a_{7,7}$?  Is there a better way than write down all the elements? It's not easy to find the formula of $a_{i,j}$
64500
Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter, and then pass them to the string method. My code looks something like this:  class PairEvaluator(HandEvaluator):   def returnArbitrary(self):     return ('ace', 'king')  pe = PairEvaluator() cards = pe.returnArbitrary() print('Two pair, {0}s and {1}s'.format(cards))   When I try to run this code, the compiler gives an IndexError: tuple index out of range. How should I structure my return value to pass it as an argument to .format()?
43916
I have much more experience programming than I do with advanced mathematics, so perhaps this is just a comfort thing with me, but I often get frustrated when I try to follow mathematical notation. Specifically, I get frustrated trying to keep track of what each variable signifies.  As a programmer, this would be completely unacceptable no matter how many comments you added explaining it:  float A(float P, float r, float n, float t) {   return P * pow(1 + r / n, n * t); }   Yet a mathematician would have no problem with this:     $A = P\ \left(1+\dfrac{r}{n}\right)^{nt}$      where   $A$ = final amount   $P$ = principal amount (initial investment)   $r$ = annual nominal interest rate (as a decimal)   $n$ = number of times the interest is compounded per year   $t$ = number of years   So why don't I ever see the following?  $\text{final_amount} = \text{principal}\; \left(1+\dfrac{\text{interest_rate}}{\text{periods_per_yr}}\right)^{\text{periods_per_yr}\cdot\text{years}}$
12929
Could someone please tell me what I'm doing wrong with this code? It is just printing 'count' anyway.  I just want a very simple prime generator (nothing fancy).   import math  def main():     count = 3     one = 1     while one == 1:         for x in range(2, int(math.sqrt(count) + 1)):             if count % x == 0:                  continue             if count % x != 0:                 print count          count += 1
87341
An array is defined to be maxmin equal if it contains at least two different elements and the number of times the maximum value occur is the same as the number of times the minimum value occur. So {11, 4, 9, 11, 8, 5 , 4, 10} is maxmin equal, because the max value 11 and min value 4 both appear two times in the array.   if the input array is {} , isMaxMinEqual should return false (array must have at least two different elements) status = false; if the input array is {2}, isMaxMinEqual should return false (array must have at least two different elements). if the input array is {1,1,1,1,1,1}, isMaxMinEqual should return false (array must have at least two different elements).  if the input array is {2,4,6,8,11}, isMaxMinEqual should return true (Both max value(11) and min value 2 appear exactly one time). if the input array is {-2,-4,-6,-8,-11}, isMaxMinEqual should return true(Both max value(-2) and min value -11 appear exaclty one time).   I wrote the class MaxMin which satisfied the above condition.  public class MaxMin {  public static void main(String args[]) {     System.out.println("The result is: " + isMaxMinEqual(new int[]{2, 2})); }  public static boolean isMaxMinEqual(int[] a) {     boolean status = false;     int count = 0;     if (a.length &lt; 2) {         status = false;         return status;     }     for (int i = 1; i &lt; a.length; i++) {         if (a[i - 1] != a[i]) {             count++;         }     }      int max = a[0];     int min = a[0];     for (int i = 0; i &lt; a.length; i++) {         if (a[i] &gt; max) {             max = a[i];         } else if (a[i] &lt; min) {             min = a[i];         }     }      int maxCount = 0;     for (int j = 0; j &lt; a.length; j++) {         if (a[j] == max) {             maxCount++;         }     }      int minCount = 0;     for (int j = 0; j &lt; a.length; j++) {         if (a[j] == min) {             minCount++;         }     }     System.out.println(count);     System.out.println("max is: " + max);     System.out.println("min is: " + min);     System.out.println("Max Count is: " + maxCount);     System.out.println("Min Count is: " + minCount);      if (maxCount == minCount &amp;&amp; count &gt;= 1) {         status = true;     }      return status; } }
18448
Let's say I have an Array of numbers: [2,3,3,4,2,2,5,6,7,2]  What is the best way to find the minimum or maximum value in that Array?  Right now, to get the maximum, I am looping through the Array, and resetting a variable to the value if it is greater than the existing value:  var myArray:Array /* of Number */ = [2,3,3,4,2,2,5,6,7,2];  var maxValue:Number = 0;  for each (var num:Number in myArray) {     if (num &gt; maxValue)         maxValue = num; }   This just doesn't seem like the best performing way to do this (I try to avoid loops whenever possible).
17043
I have a Python script which takes as input a list of integers, which I need to work with four integers at a time.  Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples.  Currently, I'm iterating over it this way: for i in range(0, len(ints), 4):     # dummy op for example code     foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]  It looks a lot like &quot;C-think&quot;, though, which makes me suspect there's a more pythonic way of dealing with this situation.  The list is discarded after iterating, so it needn't be preserved.  Perhaps something like this would be better? while ints:     foo += ints[0] * ints[1] + ints[2] * ints[3]     ints[0:4] = []  Still doesn't quite &quot;feel&quot; right, though.  :-/ Related question:
101559
A client buys 72 turkeys. On the receipt, 2 digits are missing, the first and the last. So, the total price is $\${-}67.9{-}$. Find a way to make the price of each turkey round at the second digit without trial-and-error method.  So, the answer is obviously $\$5.11$/turkey, so $\$367.92$ total. I found that with with 2-3 trial-and-error.  I bypassed his problem by using python and made a script that tries every combination and assures it is divisible by 0.01.   Please help me find a mathematical way of solving this problem.
80880
My code:  a = '2.3'   I wanted to display a as a floating point value.  Since a is a string, I tried:  float(a)   The result I got was :  2.2999999999999998   I want a solution for this problem. Please, kindly help me.  I was following .
410
I needed to create a list of lists in Python, so I typed the following: my_list = [[1] * 4] * 3  The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]    Then I changed one of the innermost values: my_list[0][0] = 5  Now my list looks like this: [[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]]    which is not what I wanted or expected. Can someone please explain what's going on, and how to get around it?