fractions

Regex - Identify Fractions

寵の児 提交于 2021-02-16 14:54:09
问题 I need to identify a fraction from a form field in a recipe database using a Regex. Ingredients will be entered in a two part form fields. Field one is the amount, Field two is the ingredient. I then need to break field one into its fractional components to input into the database. Possible entries include: 1, 1/2, 1 1/2, and any of the previous with words attached such as 1 cup, or 1/2 tbsp. the hardest I foresee would be: [2 28 oz. cans] where 2 is the number, and 28 oz. cans would be the

R - convert POSIXct to fraction of julian day

假装没事ソ 提交于 2021-02-16 14:46:41
问题 How can a date/time object in R be transformed on the fraction of a julian day? For example, how can I turn this date: date <- as.POSIXct('2006-12-12 12:00:00',tz='GMT') into a number like this > fjday [1] 365.5 where julian day is elapsed day counted from the january 1st. The fraction 0.5 means that it's 12pm, and therefore half of the day. This is just an example, but my real data covers all the 365 days of year 2006. 回答1: Since all your dates are from the same year (2006) this should be

The sum of n terms of fractional expression is an integer when it should be a float

╄→гoц情女王★ 提交于 2021-02-11 14:44:10
问题 In the following code why don't I get a valid result unless I put term = 1.0/n and not when term = 1/n . I have declared term as float, Shouldn't that be enough? #include <stdio.h> int main() { float sum = 0, term; int n, i; printf("enter the value of n:\n"); scanf("%d", &n); term = 1.0 / n; for(i = 1; i <= n; i++) { sum = term + sum; } printf("Sum = %.3f\n", sum); return 0; } 回答1: In the following code why don't I get a valid result unless I put term = 1.0/n and not when term = 1/n . I have

NumPy: convert decimals to fractions

寵の児 提交于 2021-02-07 07:37:32
问题 I compute the reverse of matrix A , for instance, import numpy as np A = np.diag([1, 2, 3]) A_inv = np.linalg.pinv(A) print(A_inv) I got, [[ 1. 0. 0. ] [ 0. 0.5 0. ] [ 0. 0. 0.33333333]] But, I want this, [[ 1. 0. 0. ] [ 0. 1/2 0. ] [ 0. 0. 1/3]] I tried np.set_printoptions , import fractions np.set_printoptions(formatter={'all':lambda x: str(fractions.Fraction(x))}) print(A_inv) but I got this, [[1 0 0] [0 1/2 0] [0 0 6004799503160661/18014398509481984]] How do I convert decimals to

R: converting fractions into decimals in a data frame

萝らか妹 提交于 2021-02-05 07:14:45
问题 I am trying to convert a data frame of numbers stored as characters in a fraction form to be stored as numbers in decimal form. (There are also some integers, also stored as char.) I want to keep the current structure of the data frame, i.e. I do not want a list as a result. Example data frame (note: the real data frame has all elements as character, here it is a factor but I couldn't figure out how to replicate a data frame with characters): a <- c("1","1/2","2") b <- c("5/2","3","7/2") c <-

Decimal to vulgar fraction conversion method- need help converting from Swift to Kotlin

跟風遠走 提交于 2021-01-29 11:06:23
问题 I found this beautiful decimal to fraction function: https://gist.github.com/natecook1000/9ecc976aaac9a035bddf I have manipulated the above for my apps necessity and would like help in making a Kotlin version. I have indeed tried making the conversion myself but I am a newbie coder period and have only been at Kotlin 3 weeks now. I don't have enough experience matching syntax and semantics between languages to do it all myself. Help appreciated :) I changed the above to output custom Unicode

Converting a floating-point decimal value to a fraction

巧了我就是萌 提交于 2020-12-09 05:00:12
问题 Given a decimal floating-point value, how can you find its fractional equivalent/approximation? For example: as_fraction(0.1) -> 1/10 as_fraction(0.333333) -> 1/3 as_fraction(514.0/37.0) -> 514/37 Is there a general algorithm that can convert a decimal number to fractional form? How can this be implemented simply and efficiently in C++? 回答1: First get the fractional part and then take the gcd. Use the Euclidean algorithm http://en.wikipedia.org/wiki/Euclidean_algorithm void foo(double input)

Foobar - Test cases not passing - Disorderly escape - 2

会有一股神秘感。 提交于 2020-03-28 06:58:34
问题 0/10 test cases are passing. Here is the challenge description: (to keep formatting nice - i put the description in a paste bin) Link to challenge description: https://pastebin.com/UQM4Hip9 Here is my trial code - PYTHON - (0/10 test cases passed) from math import factorial from collections import Counter from fractions import gcd def cycle_count(c, n): cc=factorial(n) for a, b in Counter(c).items(): cc//=(a**b)*factorial(b) return cc def cycle_partitions(n, i=1): yield [n] for i in range(i,