Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:18:22+00:00 2026-06-17T13:18:22+00:00

I want to solve this excercise: http://code.google.com/codejam/contest/351101/dashboard#s=p0 using F#. I am new to functional

  • 0

I want to solve this excercise: http://code.google.com/codejam/contest/351101/dashboard#s=p0 using F#.

I am new to functional programming and F# but I like the concept and the language a lot. And I love the codejam excercise too it looks so easy but real life. Could somebody point me out a solution?

At the moment I have written this code which is just plain imperative and looks ugly from the functional perspective:

(*
    C - Credit
    L - Items
    I - List of Integer, wher P is single integer

    How does the data look like inside file
    N
    [...
    * Money
    * Items in store
    ...]
*)

    let lines = System.IO.File.ReadAllLines("../../../../data/A-small-practice.in")
    let CBounds c = c >= 5 && c <= 1000
    let PBounds p = p >= 1 && p <= 1000

    let entries = int(lines.[0]) - 1
    let mutable index = 1   (* First index is how many entries*)
    let mutable case = 1

    for i = 0 to entries do
        let index = (i*3) + 1
        let C = int(lines.[index])
        let L = int(lines.[index+1])
        let I = lines.[index+2]    
        let items = I.Split([|' '|]) |> Array.map int    
        // C must be the sum of some items

        // Ugly imperative way which contains duplicates
        let mutable nIndex = 0
        for n in items do
            nIndex <- nIndex + 1
            let mutable mIndex = nIndex
            for m in items.[nIndex..] do            
                mIndex <- mIndex + 1
                if n + m = C then do
                    printfn "Case #%A: %A %A" case nIndex mIndex
                    case <- case + 1

I would like to find out items which add up to C value but not in a usual imperative way – I want functional approach.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:18:23+00:00Added an answer on June 17, 2026 at 1:18 pm

    You don’t specify how you would solve the problem, so it’s hard to give advices.

    Regarding reading inputs, you can express it as a series of transformation on Seq. High-order functions from Seq module are very handy:

    let data = 
       "../../../../data/A-small-practice.in"
       |> System.IO.File.ReadLines
       |> Seq.skip 1
       |> Seq.windowed 3
       |> Seq.map (fun lines -> let C = int(lines.[0])
                                let L = int(lines.[1])
                                let items = lines.[2].Split([|' '|]) |> Array.map int
                                (C, L, items))
    

    UPDATE:

    For the rest of your example, you could use sequence expression. It is functional enough and easy to express nested computations:

     let results = 
         seq { 
              for (C, _, items) in data do
                for j in 1..items.Length-1 do
                  for i in 0..j-1 do
                    if items.[j] + items.[i] = C then yield (i, j)
         }
    
     Seq.iteri (fun case (i, j) -> printfn "Case #%A: %A %A" case i j) results
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to solve this problem in Scala. My code: def dividers(n: Int) =
I am new to prolog and i want to solve this problem. Suppose I
I want to solve this linear programming (simplex) problem using MATLAB 7, but it
I want to solve this issue in query @ for Ex : id ip
I'm trying for hours now to solve this problem.. So.. I want to use
I've been searching for days to solve this, but with no success. I want
I want to solve a system of n linear equations containing n variables using
I have a little problem: I want to solve this problem with OCaml, so
I want to solve this CodeChef challenge: Suppose We are given an array A
How to solve this using scala Actors: I have a program that finds out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.