Gives grades according to marks - visual-studio

i have SSRS report i want to give "Accepted" if value between 50 and 65 and gives "Good" if value between 65 and 75 and gives "V good" if value between 75 and 85 and gives "Excellent" if value between 85 and 100
how can i do it my code :
=(IIF(Fields!marks.Value >50, "fail", 0)) And (IIF(Fields!marks.Value <65, "Accepted", 0))

You could use the Switch function to accomplish this:
=Switch(
Fields!marks.Value < 50, "fail",
Fields!marks.Value < 65, "accepted",
Fields!marks.Value < 75, "good",
Fields!marks.Value < 85, "v good",
True, "excellent"
)

what you want is a switch statement:
=switch(
Fields!marks.Value<50,"Fail",
Fields!marks.Value>= 50 and Fields!marks.Value<65,"Accepted",
Fields!marks.Value>= 65 and Fields!marks.Value<75,"Good",
Fields!marks.Value>= 75 and Fields!marks.Value<85,"V Good",
Fields!marks.Value>= 85 and Fields!marks.Value<100,"Excellent",
,"")

Related

Lua sort table value ascending

I have a table I need to sort sel_notes[i].Pitch to ascending order.
These are the selected midi notes in a midi editor.
sel notes table :
sel_notes = {}
sel_notes[pitch] = {Pitch = pitch, Idx = i}
sel_notes[i] = {Pitch = pitch, Idx = i}
sel_notes table gives this using table.save-1.0.lua:
return {
-- Table: {1}
{
[64]={2},
[65]={3},
[66]={4},
[67]={5},
[52]={6},
[69]={7},
[68]={8},
[55]={9},
[56]={10},
[57]={11},
[58]={12},
[59]={13},
[60]={14},
[61]={15},
[62]={16},
[63]={17},
},
-- Table: {2}
{
["Pitch"]=63,
["Idx"]=64,
},
-- Table: {3}
{
["Pitch"]=52,
["Idx"]=65,
},
-- Table: {4}
{
["Pitch"]=58,
["Idx"]=66,
},
-- Table: {5}
{
["Pitch"]=52,
["Idx"]=67,
},
-- Table: {6}
{
["Pitch"]=52,
["Idx"]=67,
},
-- Table: {7}
{
["Pitch"]=58,
["Idx"]=69,
},
-- Table: {8}
{
["Pitch"]=63,
["Idx"]=68,
},
-- Table: {9}
{
["Pitch"]=52,
["Idx"]=55,
},
-- Table: {10}
{
["Pitch"]=58,
["Idx"]=56,
},
-- Table: {11}
{
["Pitch"]=63,
["Idx"]=57,
},
-- Table: {12}
{
["Pitch"]=58,
["Idx"]=69,
},
-- Table: {13}
{
["Pitch"]=63,
["Idx"]=59,
},
-- Table: {14}
{
["Pitch"]=52,
["Idx"]=60,
},
-- Table: {15}
{
["Pitch"]=52,
["Idx"]=61,
},
-- Table: {16}
{
["Pitch"]=63,
["Idx"]=62,
},
-- Table: {17}
{
["Pitch"]=63,
["Idx"]=68,
},
}
I need the table sorted so if I do this
for 1 = 1, 15 do
note = sel_notes[i].Pitch
index = sel_notes[i].Idx
print(note,index)
end
I will get this:
52 55
52 60
52 61
52 65
52 67
58 56
58 58
58 63
58 66
58 69
63 57
63 59
63 62
63 64
63 68
So then I'll be able to change the pitch value of the notes so they match the pitch value of another table that has the chord notes.
So:
pitch 52 to 55
pitch 58 to 59
pitch 63 to 62
You can use the table.sort method.
The first argument is a table and the second (optional) is a function that returns whether its first argument is smaller than the second.
Of course there's the problem of the rather weird table structure, so you need to get rid of the first element, which is meaningless. The easiest way for doing this would be using table.remove.
So something like
local function sort(tab)
table.remove(tab, 1) -- Remove first element
table.sort(tab, function(a, b)
return true -- Your sorting criterion goes here
end)
end

Strange behaviour of the delete element within nested slice in golang

I am working on a golang project. I am trying to maintain a slice from a existing slice where my new slice does not contain the existing slice element. I have tried the code like :
package main
import (
"fmt"
"reflect"
)
func main(){
savedArr := make(map[string][]int)
newArr := make(map[string][]int)
days := []string{"saturday", "friday", "sunday"}
newSpotsArr := []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 101}
savedArr["saturday"] = []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 101}
savedArr["friday"] = []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 101}
for _, day := range days{
newArr[day] = newSpotsArr
}
for day, newSpots := range newArr{
fmt.Println("day", day)
fmt.Println("Top within loop", newSpots)
for _, oldSpot := range savedArr[day]{
exists, idx := InArray(oldSpot, newSpots)
if exists {
newSpots = append(newSpots[:idx], newSpots[idx + 1:]...)
}
}
fmt.Println("Bottom within loop", newSpots)
}
}
func InArray(val interface{}, array interface{}) (exists bool, index int) {
exists = false
index = -1
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
index = i
exists = true
return
}
}
}
return
}
Its output is like:
day saturday
Top within loop [10 20 30 40 50 60 70 80 90 100 101]
Bottom within loop []
day friday
Top within loop [101 101 101 101 101 101 101 101 101 101 101]
Bottom within loop [101 101 101 101 101 101 101 101 101 101]
day sunday
Top within loop [101 101 101 101 101 101 101 101 101 101 101]
Bottom within loop [101 101 101 101 101 101 101 101 101 101 101]
finalArr map[friday:[101 101 101 101 101 101 101 101 101 101] sunday:[101 101 101 101 101 101 101 101 101 101 101] saturday:[]]
I am not able to understand how it is working. I was expecting its output should be like:
day saturday
Top within loop [10 20 30 40 50 60 70 80 90 100 101]
Bottom within loop []
day friday
Top within loop [10 20 30 40 50 60 70 80 90 100 101]
Bottom within loop []
day sunday
Top within loop [10 20 30 40 50 60 70 80 90 100 101]
Bottom within loop [10 20 30 40 50 60 70 80 90 100 101]
finalArr map[friday:[] sunday:[10 20 30 40 50 60 70 80 90 100 101] saturday:[]]
Can anybody tell how it is working?
And how I can achieve my desired output
The code is not working as expected because all newArr[day] has a same underlying array as newSpotsArr. You need to make a copy of newSportsArr by make a new slice and copy data into it:
for _, day := range days {
newArr[day] = make([]int, len(newSpotsArr))
copy(newArr[day], newSpotsArr)
}
Playground: https://play.golang.org/p/kv-B9NnKqVd
UPDATE:
slice in Go is a reference type. Under the hood, slice keeps 3 things: len,cap,and a ptr. The ptr is a pointer to a memory (underlying array). when you run newArr[day]=newSpotsArr, all three value is copied, which means, newArr[day] points to the same underlying array as newSpotsArr. So if you run newSpotsArr[0]=-100, all slices will see the that change.
See also: https://blog.golang.org/go-slices-usage-and-internals

modify json file using cJSON library

what i want to do is reading a json format file and modify it then writing the modified content to file.
55 cJSON *root,*basicpara;
56 char *out;
57
58 root = dofile("basicparameter.cfg");
59 out = cJSON_Print(root);
60 printf("before modify:%s\n",out);
61 free(out);
62 basicpara = cJSON_GetObjectItem(root,"basicparameter");
63 cJSON_GetObjectItem(basicpara,"mode")->valueint = 0;
64 cJSON_GetObjectItem(basicpara,"TimeoutPoweron")->valueint = 10;
65
66 out = cJSON_Print(root);
67 printf("after modify:%s\n",out);
68 free(out);
69 //write_file("basicparameter.cfg",out);
70 cJSON_Delete(root);
i am confused why both contents are the same...
before modify:{
"basicparameter": {
"mode": 1,
"nBefore": 2,
"nAfter": 2,
"LuxAutoOn": 50,
"LuxAutoOff": 16,
"TimeoutPoweron": 30
}
}
after modify:{
"basicparameter": {
"mode": 1,
"nBefore": 2,
"nAfter": 2,
"LuxAutoOn": 50,
"LuxAutoOff": 16,
"TimeoutPoweron": 30
}
}
Please use the cJSON_SetNumberValue macro for setting the number. The problem is, that you are only setting the valueint property but printing relies on the valuedouble property.
Having both valueint and valuedouble in cJSON was a terrible design decision and will probably confuse many people in the future as well.

Different ways to create this String in Ruby?

Disclaimer: Yes, this is homework and yes, I have already solved it.
Task: Create the String "0 10 20 30 40 50 60 70 80 90 100" <- Note, no whitespace
Obviously not using direct assignment, but using tools such as loops, ranges, splitting and such. I already finished this using a 10-increment loop, and I am pretty sure there are way more intelligent ways to solve it and I am curious about the alternatives.How would you build a String like this?
Yes there is using Range#step and Array#*:
(0..100).step(10).to_a * " "
# => "0 10 20 30 40 50 60 70 80 90 100"
Second version using Range#step:
(0..100).step(10).to_a.join(' ')
# "0 10 20 30 40 50 60 70 80 90 100"
Just to be different and not use #step:
(0..10).map{|x| x * 10}.join(' ')
Numeric#step would work, too (here with a Ruby 2.1 style hash argument)
0.step(by: 10).take(11).join(' ')
#=> "0 10 20 30 40 50 60 70 80 90 100"
Just to be contrary, here are some using times with map, with_object and inject:
10.times.map{ |i| "#{ 10 * (i + 1) }" }.join(' ') # => "10 20 30 40 50 60 70 80 90 100"
10.times.with_object([]) { |i, o| o << "#{ 10 * (i + 1) }" }.join(' ') # => "10 20 30 40 50 60 70 80 90 100"
10.times.inject([]) { |o, i| o << "#{ 10 * (i + 1) }" }.join(' ') # => "10 20 30 40 50 60 70 80 90 100"

Processing Chromosomal Data in Ruby

Say I have a file of chromosomal data I'm processing with Ruby,
#Base_ID Segment_ID Read_Depth
1 100
2 800
3 seg1 1900
4 seg1 2700
5 1600
6 2400
7 200
8 15000
9 seg2 300
10 seg2 400
11 seg2 900
12 1000
13 600
...
I'm sticking each row into a hash of arrays, with my keys taken from column 2, Segment_ID, and my values from column 3, Read_Depth, giving me
mr_hashy = {
"seg1" => [1900, 2700],
"" => [100, 800, 1600, 2400, 200, 15000, 1000, 600],
"seg2" => [300, 400, 900],
}
A primer, which is a small segment that consists of two consecutive rows in the above data, prepends and follows each regular segment. Regular segments have a non-empty-string value for Segment_ID, and vary in length, while rows with an empty string in the second column are parts of primers. Primer segments always have the same length, 2. Seen above, Base_ID's 1, 2, 5, 6, 7, 8, 12, 13 are parts of primers. In total, there are four primer segments present in the above data.
What I'd like to do is, upon encountering a line with an empty string in column 2, Segment_ID, add the READ_DEPTH to the appropriate element in my hash. For instance, my desired result from above would look like
mr_hashy = {
"seg1" => [100, 800, 1900, 2700, 1600, 2400],
"seg2" => [200, 15000, 300, 400, 900, 1000, 600],
}
hash = Hash.new{|h,k| h[k]=[] }
# Throw away the first (header) row
rows = DATA.read.scan(/.+/)[1..-1].map do |row|
# Throw away the first (entire row) match
row.match(/(\d+)\s+(\w+)?\s+(\d+)/).to_a[1..-1]
end
last_segment = nil
last_valid_segment = nil
rows.each do |base,segment,depth|
if segment && !last_segment
# Put the last two values onto the front of this segment
hash[segment].unshift( *hash[nil][-2..-1] )
# Put the first two values onto the end of the last segment
hash[last_valid_segment].concat(hash[nil][0,2]) if last_valid_segment
hash[nil] = []
end
hash[segment] << depth
last_segment = segment
last_valid_segment = segment if segment
end
# Put the first two values onto the end of the last segment
hash[last_valid_segment].concat(hash[nil][0,2]) if last_valid_segment
hash.delete(nil)
require 'pp'
pp hash
#=> {"seg1"=>["100", "800", "1900", "2700", "1600", "2400"],
#=> "seg2"=>["200", "15000", "300", "400", "900", "1000", "600"]}
__END__
#Base_ID Segment_ID Read_Depth
1 100
2 800
3 seg1 1900
4 seg1 2700
5 1600
6 2400
7 200
8 15000
9 seg2 300
10 seg2 400
11 seg2 900
12 1000
13 600
Second-ish refactor. I think this is clean, elegant, and most of all complete. It's easy to read with no hardcoded field lengths or ugly RegEx. I vote mine as the best! Yay! I'm the best, yay! ;)
def parse_chromo(file_name)
last_segment = ""
segments = Hash.new {|segments, key| segments[key] = []}
IO.foreach(file_name) do |line|
next if !line || line[0] == "#"
values = line.split
if values.length == 3 && last_segment != (segment_id = values[1])
segments[segment_id] += segments[last_segment].pop(2)
last_segment = segment_id
end
segments[last_segment] << values.last
end
segments.delete("")
segments
end
puts parse_chromo("./chromo.data")
I used this as my data file:
#Base_ID Segment_ID Read_Depth
1 101
2 102
3 seg1 103
4 seg1 104
5 105
6 106
7 201
8 202
9 seg2 203
10 seg2 204
11 205
12 206
13 207
14 208
15 209
16 210
17 211
18 212
19 301
20 302
21 seg3 303
21 seg3 304
21 305
21 306
21 307
Which outputs:
{
"seg1"=>["101", "102", "103", "104", "105", "106"],
"seg2"=>["201", "202", "203", "204", "205", "206", "207", "208", "209", "210", "211", "212"],
"seg3"=>["301", "302", "303", "304", "305", "306", "307"]
}
Here's some Ruby code (nice practice example :P). I'm assuming fixed-width columns, which appears to be the case with your input data. The code keeps track of which depth values are primer values until it finds 4 of them, after which it will know the segment id.
require 'pp'
mr_hashy = {}
primer_segment = nil
primer_values = []
while true
line = gets
if not line
break
end
base, segment, depth = line[0..11].rstrip, line[12..27].rstrip, line[28..-1].rstrip
primer_values.push(depth)
if segment.chomp == ''
if primer_values.length == 6
for value in primer_values
(mr_hashy[primer_segment] ||= []).push(value)
end
primer_values = []
primer_segment = nil
end
else
primer_segment = segment
end
end
PP::pp(mr_hashy)
Output on input provided:
{"seg1"=>["100", "800", "1900", "2700", "1600", "2400"],
"seg2"=>["200", "15000", "300", "400", "900", "1000"]}

Resources