TCL Sort program without using lsort [closed] - sorting

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
using any sorting technique
array Variable arr contained :
arr { 1 5 2 9 3 8 10 6}
Without using lsort
finally o/p to be in same array variable :
arr { 1 2 3 5 6 8 9 10}

While we won't give you the answer, we can suggest a few useful things. For example, you compare two elements of a list (at $idx1 and $idx2) with this:
string compare [lindex $theList $idx1] [lindex $theList $idx2]
And you might use this procedure to swap those two elements:
proc swap {nameOfListVar idx1 idx2} {
upvar 1 $nameOfListVar theList
set tmp [lindex $theList $idx1]
lset theList $idx1 [lindex $theList $idx2]
lset theList $idx2 $tmp
return
}
Which you'd call like:
# Pass the list variable *name*
swap theList $idx1 $idx2
Generally, you can do a sorting algorithm like this:
While the list is not sorted,
find a pair of elements in the wrong order and swap them.
Everything else is optimization. (Bear in mind that real Tcl programs just use lsort because it implements an efficient algorithm with many good properties, and the rest either don't need sorting or delegate the problem to a database engineā€¦)

Related

using for-loop to remove maximum and minimum values from a dictionary [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
how can I use for loop to delete maximum and minimum values from a dictionary. for instance if you have a dictionary grades = {sam: [23,43,55]}, peter: [66,55,44], sarah: [99,55,77]}. how can I remove only the maximum and minimum values?
im new to coding and cannot figure it out. Language is python
Hi you can try something like this:
# Create dictionary
dic = {'sam': [23,43,55], 'peter': [66,55,44], 'sarah': [99,55,77]}
# Use for loop to iterate
for key, value in dic.items():
# Find maximum value and it's index
max_value = max(value)
max_index = value.index(max_value)
# Delete maximum value
del dic[key][max_index]
# Find minimum value and it's index
min_value = min(value)
min_index = value.index(min_value)
# Delete miniumum value
del dic[key][min_index]
# Print output
print(dic)
Let me know if you don't understand anything. Cheers

How to sort array contain double value in Kotlin? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have below kind of array,
val mArrayList: ArrayList<String> = ArrayList()
mArrayList.add("10-12 YRS")
mArrayList.add("2-3 YRS")
mArrayList.add("4-5 YRS")
mArrayList.add("7-10 YRS")
mArrayList.add("5-7 YRS")
I have the above kind of ArrayList and I want to sort this ArrayList.
How to sort this kind of array?
Can anyone please help?
In Kotlin, you have the sortedBy/sortBy methods to sort such a list. The first one returns a new list that is sorted, the second sorts the receiver list in-place.
The lambda you pass it allows you to extract the value you want to use for comparison.
For instance, you can use this to sort by the first integer in your elements:
val sorted = mArrayList.sortedBy { it.split("-").first().toInt() }
Or if you want to sort your list in place:
mArrayList.sortBy { it.split("-").first().toInt() }
In both cases, it.split("-").first().toInt() splits the string into 2 parts (before and after the dash), and converts the first part into an integer (which is comparable).
I believe, this is the sort you are looking for:
mArrayList.sortBy { it.split("-", limit = 2)[0].toInt() }
println(mArrayList) //[2-3 YRS, 4-5 YRS, 5-7 YRS, 7-10 YRS, 10-12 YRS]

how to count arrays within an array in ruby and return this value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to count the arrays themselves not the elements. so say i had the following
arrays = [["1 2"],["3 4"],["5 6"],["6 7"]]
i am then trying to find a way to return only one of these arrays as a set of instructions if asked for?
Your example showed a multi-dimensional array, which is an array of arrays.
To count the number of arrays in this 2 dimensional array, in other words the number of elements, you could use the following methods.
arrays = [["1 2"],["3 4"],["5 6"],["6 7"]]
arrays.length
arrays.size
arrays.count
If you would like to return one of them you simply reference the element
arrays[0]
=> Returns ["1 2"]
If you want to return a random element,
arrays.sample
=> Returns a random array

Interpreting "takes as input a sorted list of N integers output will be sorted squared integer list" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
how would you interpret that sentence in order to start coding. do you take it as the user inputs the number into an array or does it come from a text document for example and just scan it.
I already did it for both but I only have one submission so I have to make it count
I think the requirement is very clear - you need to write a function that receives a sorted list of integers, and returns a list of their squares.
E.g., in Java:
public List<Integer> squareList (List<Integer list) {
List<Integer> retVal = new ArrayList<>(list.size());
for (int item : list) {
retVal.add(item * item);
}
Collections.sort(retVal);
return retVal;
}

Ruby - Equivalent to foreach [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm working on a project in RMXP with Ruby scripts.
I create a table with id maps. I am now looking to go the table such that if the player enters one of the maps of the table, things happen.
Example of action: tMaps = [006, 008, 009]
If the hero is on the map 004, 005 ... it nothing happens If the hero
is on the map 006, Action
I hope this is feasible. If not, what other means I have in Ruby to find the maps and to assign actions?
PS I've traveled a bit, this wiki : http://fr.wikibooks.org/wiki/Programmation_Ruby/Contr%C3%B4le
Your list is wrong, I suppose you want integer : [6, 8, 9] not [006, 008, 009] (this is octal form)
tMaps = [6, 8, 9]
tMaps.each do
|value| puts 'value : '+value.to_s
end
# or :
for value in tMaps
puts 'value : '+value.to_s
end

Resources