logo
down
shadow

Add the previous element for each element in a list in OCaml


Add the previous element for each element in a list in OCaml

By : Victor Espinoza
Date : November 20 2020, 11:01 PM
hop of those help? I want to do something like this:
code :
let sumsum l =
  let rec aux a' acc = function
    | []    -> List.rev acc
    | a::tl -> let b=a+a' in aux b (b::acc) tl
  in
  aux 0 [] l
;;
# sumsum [1;2;3;4;5];;
- : int list = [1; 3; 6; 10; 15]


Share : facebook icon twitter icon
Checking if one element is equal to any element in a list in OCaml?

Checking if one element is equal to any element in a list in OCaml?


By : zak ahmed
Date : March 29 2020, 07:55 AM
wish helps you So what I have this list of ints, let's say it is
code :
$ ocaml
        OCaml version 4.01.0

# List.mem;;
- : 'a -> 'a list -> bool = <fun>
# List.mem 3 [1;2;3];;
- : bool = true
# List.mem 8 [1;2;3];;
- : bool = false
Python: Split list in subsets if the current element is minor than previous element

Python: Split list in subsets if the current element is minor than previous element


By : Juan Carlos SC
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Here's a comprehension-ish approach. But while you could cram this into one long expression, why on Earth would you want to?
code :
>>> my_list = [1, 2, 3, 1, 2, 1, 2, 3]
>>> brks = [i for i in range(1,len(my_list)) if my_list[i] < my_list[i-1]]
>>> [my_list[x:y] for x,y in zip([0]+brks,brks+[None])]
[[1, 2, 3], [1, 2], [1, 2, 3]]
For each element in an array, if the element is less than its previous element, increase it till the previous element wi

For each element in an array, if the element is less than its previous element, increase it till the previous element wi


By : Jan Toť
Date : March 29 2020, 07:55 AM
I wish this helpful for you Suppose I have an array: list1 = [8, 5, 3, 1, 1, 10, 15, 9] Now if the element is less than its previous element, increase it till the previous element with one. , The following code will create a new list with the required output
code :
l1 = [8, 5, 3, 1, 1, 10, 15, 9]
l = [l1[0]]
c=[0]         # cost / difference list

for i in range(len(l1)-1):
    if l1[i+1] < l1[i]:
        l.append(l1[i]+1)
        c.append(l1[i]+1-l1[i+1])
    else:
        l.append(l1[i+1])
        c.append(0)
print(l)
[8, 9, 6, 4, 1, 10, 15, 16]
[0, 4, 3, 3, 0,  0,  0,  7]
How to find next element that matches a previous element within a tolerance in a list of lists?

How to find next element that matches a previous element within a tolerance in a list of lists?


By : user2953682
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You almost had it, just create a new loop inside with j that starts at i, checks for a match along the way and ends when the first match is found.
code :
biglist = [
    [[25368, 22348], [22348, 21234], [21234, 17750], [17750, 15924], [15924, 14490], [14490, 12780], [12780, 9418], [9418, 7460], [7460, 4884], [4884, 4340]], 
    [[22390, 21242], [10140, 4260], [4260, 2686], [2686, 438]], 
    [[14044, 8726], [8762, 4144], [4144, 1420]], [[5817, 5097], [5590, 5530], [5304, 2729], [5097, 4430], [3450, 2489], [2729, 1676] , [2489, 1618]]
]

for sublist in biglist:
  matched = set()
  for i, element in list(enumerate(sublist))[:-1]:
    found = False
    for j in range(i+1, len(sublist)):
      if abs(sublist[i][1] - sublist[j][0]) < 10:
        print(sublist[i], "matches",  sublist[j], "within tolerance of 10 - skipped elements", sublist[i+1:j])
        matched.add(tuple(sublist[j]))
        found = True
        break
    if not found and tuple(sublist[i]) not in matched:
      print(sublist[i], "has no matches")
[25368, 22348] matches [22348, 21234] within tolerance of 10 - skipped elements []
[22348, 21234] matches [21234, 17750] within tolerance of 10 - skipped elements []
[21234, 17750] matches [17750, 15924] within tolerance of 10 - skipped elements []
[17750, 15924] matches [15924, 14490] within tolerance of 10 - skipped elements []
[15924, 14490] matches [14490, 12780] within tolerance of 10 - skipped elements []
[14490, 12780] matches [12780, 9418] within tolerance of 10 - skipped elements []
[12780, 9418] matches [9418, 7460] within tolerance of 10 - skipped elements []
[9418, 7460] matches [7460, 4884] within tolerance of 10 - skipped elements []
[7460, 4884] matches [4884, 4340] within tolerance of 10 - skipped elements []
[22390, 21242] has no matches
[10140, 4260] matches [4260, 2686] within tolerance of 10 - skipped elements []
[4260, 2686] matches [2686, 438] within tolerance of 10 - skipped elements []
[14044, 8726] has no matches
[8762, 4144] matches [4144, 1420] within tolerance of 10 - skipped elements []
[5817, 5097] matches [5097, 4430] within tolerance of 10 - skipped elements [[5590, 5530], [5304, 2729]]
[5590, 5530] has no matches
[5304, 2729] matches [2729, 1676] within tolerance of 10 - skipped elements [[5097, 4430], [3450, 2489]]
[3450, 2489] matches [2489, 1618] within tolerance of 10 - skipped elements [[2729, 1676]]
What is the complexity of List.nth in OCaml? Can we find the nth element in OCaml in O(1)?

What is the complexity of List.nth in OCaml? Can we find the nth element in OCaml in O(1)?


By : Omar Zarate
Date : March 29 2020, 07:55 AM
will help you I found the manual of OCaml List module did not say how the List.nth does. Does it cost O(1) or O(n) like some plain recursive implementation. If List.nth is O(n), can we write a function to find the nth element in O(1) time in OCaml? , Standard lists are "singly-linked", so it will always be O(n).
shadow
Privacy Policy - Terms - Contact Us © soohba.com