Hide all months except current - show-hide

I have a sheet with columns for each month. Column C is January (cell C1 = 1), Column D is February (cell D1 = 2), etc.
I'm trying to hide all of the month columns except the current month. I think I'm getting close but the logic escapes me.
Suggestions?...JD
function HideTheMonths(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Sum Remaining");
var date = new Date();
var mo1 = date.getMonth();
var mons = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var curmons = mons[mo1];
for (var x = 3; x < 15; x++){
var c = s.getRange(1, x)
if (curmons = c) {
var datarange = s.hideColumns(x);
}
}
}

Related

RXJS: alternately combine elements of streams

I'd like to alternately combine elements of multiple streams:
var print = console.log.bind(console);
var s1 = Rx.Observable.fromArray([1, 1, 5]);
var s2 = Rx.Observable.fromArray([2, 9]);
var s3 = Rx.Observable.fromArray([3, 4, 6, 7, 8]);
alternate(s1, s2, s3).subscribe(print); // 1, 2, 3, 1, 9, 4, 5, 6, 7, 8
How looks the function definition of alternate?
Use zip and concatMap when working on observables that were created from arrays (as in your example), or zip and flatMap when working on observables that are inherently asynchronous.
Rx.Observable
.zip(s1, s2, s3, function(x,y,z) { return [x,y,z]; })
.concatMap(function (list) { return Rx.Observable.from(list); })
.subscribe(print); // 1, 2, 3, 1, 9, 4
Notice that this doesn't proceed anymore once one of the source observables completes. That's because zip is strictly "balanced" and it waits until all the sources have a matching event. What you want is a somewhat loose version of zip when dealing with completed sources.
If there is a value (for example undefined) that is not emitted by the source observables, this solution works:
var concat = Rx.Observable.concat;
var repeat = Rx.Observable.repeat;
var zipArray = Rx.Observable.zipArray;
var fromArray = Rx.Observable.fromArray;
var print = console.log.bind(console);
var s1 = fromArray([1, 1, 5]);
var s2 = fromArray([2, 9]);
var s3 = fromArray([3, 4, 6, 7, 8]);
alternate(s1, s2, s3).subscribe(print);
function alternate() {
var sources = Array.slice(arguments).map(function(s) {
return concat(s, repeat(undefined))
});
return zipArray(sources)
.map(function(values) {
return values.filter(function(x) {
return x !== undefined;
});
}).takeWhile(function(values) {
return values.length > 0;
}).concatMap(function (list) { return fromArray(list); })
}
Same example in ES6:
const {concat, repeat, zipArray, fromArray} = Rx.Observable;
var print = console.log.bind(console);
var s1 = fromArray([1, 1, 5]);
var s2 = fromArray([2, 9]);
var s3 = fromArray([3, 4, 6, 7, 8]);
alternate(s1, s2, s3).subscribe(print);
function alternate(...sources) {
return zipArray(sources.map( (s) => concat(s, repeat(undefined)) ))
.map((values) => values.filter( (x) => x !== undefined ))
.takeWhile( (values) => values.length > 0)
.concatMap( (list) => fromArray(list) )
}

ColdFusion VIN number validation code

I am trying to convert this PHP validation code to ColdFusion. However, I cannot get my CF version to correctly validate a VIN. I am hoping someone can shed some light on what I'm missing.
<cfscript>
function isVIN(v) {
var i = "";
var d = "";
var checkdigit = "";
var sum = 0;
var weights = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2];
var transliterations = {
a = 1,
b = 2,
c = 3,
d = 4,
e = 5,
f = 6,
g = 7,
h = 8,
j = 1,
k = 2,
l = 3,
m = 4,
n = 5,
p = 7,
r = 9,
s = 2,
t = 3,
u = 4,
v = 5,
w = 6,
x = 7,
y = 8,
z = 9
};
if (! REFindNoCase("^([\w]{3})[A-Z]{2}\d{2}([A-Z]{1}|\d{1})([\d{1}|X{1})([A-Z]+\d+|\d+[A-Z]+)\d{5}$", ARGUMENTS.v)) {
return false;
}
if (Len(ARGUMENTS.v) != 17) {
return false;
}
for (i = 1; i <= Len(ARGUMENTS.v); i++) {
d = Mid(ARGUMENTS.v, i, 1);
if (! isNumeric(d)) {
sum += transliterations[d] * weights[i];
} else {
sum += d * weights[i];
}
}
checkdigit = sum % 11;
if (checkdigit == 10) {
checkdigit = "x";
}
if (checkdigit == Mid(ARGUMENTS.v,8,1)) {
return true;
}
return false;
}
</cfscript>
(There is a VIN validation function at CFLib.org, but it doesn't work either).
Your function has two issues.
First, the regex is incorrect. Here's a simplified working version:
^[A-Z\d]{3}[A-Z]{2}\d{2}[A-Z\d][\dX](?:[A-Z]+\d+|\d+[A-Z]+)\d{5}$
Note: As per Adam's answer there's a simpler pattern than this.
Second, in your checkdigit comparison the Mid is one out - it seems the 8 should be a 9.
(Presumably this is a language conversion issue due to PHP being 0-indexed whilst CFML is 1-indexed.)
With both of these fixed, the modified function returns true for the VIN WAUBA24B3XN104537 (which is the only sample VIN I could find in a quick search).
Actually the regex is slightly wrong still. I think #PeterBoughton fixed the syntax, but it wasn't actually valid for the task at hand.
Here's the revised section of code, with suitable comments:
var vinRegex = "(?x) ## allow comments
^ ## from the start of the string
## see http://en.wikipedia.org/wiki/Vehicle_Identification_Number for VIN spec
[A-Z\d]{3} ## World Manufacturer Identifier (WMI)
[A-Z\d]{5} ## Vehicle decription section (VDS)
[\dX] ## Check digit
[A-Z\d] ## Model year
[A-Z\d] ## Plant
\d{6} ## Sequence
$ ## to the end of the string
";
if (! REFindNoCase(vinRegex, arguments.v)) {
return false;
}
This could be dramatically simplified to just this:
^[A-Z\d]{8}[\dX][A-Z\d]{2}\d{6}$
Using either of these also removes the requirement for the length check, as the regex will enforce that too.
Test code for this modification:
for (vin in [
"1GNDM19ZXRB170064",
"1FAFP40634F172825"
]){
writeOutput("#vin#: #isVin(vin)#<br />");
}
I'm gonna update CFLib with the verbose version, as it's easier to understand what's going on, and marry-up to the spec.

How can I bind group by query to DataGridview in linq?

I want bind the below query to DataGridview.
how can I it?
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var numberGroups =
from num in numbers
group num by num % 5 into numGroup
select new { Remainder = numGroup.Key, Numbers = numGroup };
How do you want it represented in the DGV since Numbers could contain more than one item how would this go into a DGV row?
Alternatively this should work:
var numberGroups = from num in numbers
group num by num % 5 into numGroup
select new { Remainder = numGroup.Key, Count = numGroup.Count() };
yourGridView.DataSource = numberGroups.ToList();
yourGridView.DataBind(); //don't use if WinForm DGV

LINQ Grouping: Is there a cleaner way to do this without a for loop

I am trying to create a very simple distribution chart and I want to display the counts of tests score percentages in their corresponding 10's ranges.
I thought about just doing the grouping on the Math.Round((d.Percentage/10-0.5),0)*10 which should give me the 10's value....but I wasn't sure the best way to do this given that I would probably have missing ranges and all ranges need to appear even if the count is zero. I also thought about doing an outer join on the ranges array but since I'm fairly new to Linq so for the sake of time I opted for the code below. I would however like to know what a better way might be.
Also note: As I tend to work with larger teams with varying experience levels, I'm not all that crazy about ultra compact code unless it remains very readable to the average developer.
Any suggestions?
public IEnumerable<TestDistribution> GetDistribution()
{
var distribution = new List<TestDistribution>();
var ranges = new int[] { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110 };
var labels = new string[] { "0%'s", "10%'s", "20%'s", "30%'s", "40%'s", "50%'s", "60%'s", "70%'s", "80%'s", "90%'s", "100%'s", ">110% "};
for (var n = 0; n < ranges.Count(); n++)
{
var count = 0;
var min = ranges[n];
var max = (n == ranges.Count() - 1) ? decimal.MaxValue : ranges[n+1];
count = (from d in Results
where d.Percentage>= min
&& d.Percentage<max
select d)
.Count();
distribution.Add(new TestDistribution() { Label = labels[n], Frequency = count });
}
return distribution;
}
// ranges and labels in a list of pairs of them
var rangesWithLabels = ranges.Zip(labels, (r,l) => new {Range = r, Label = l});
// create a list of intervals (ie. 0-10, 10-20, .. 110 - max value
var rangeMinMax = ranges.Zip(ranges.Skip(1), (min, max) => new {Min = min, Max = max})
.Union(new[] {new {Min = ranges.Last(), Max = Int32.MaxValue}});
//the grouping is made by the lower bound of the interval found for some Percentage
var resultsDistribution = from c in Results
group c by
rangeMinMax.FirstOrDefault(r=> r.Min <= c.Percentage && c.Percentage < r.Max).Min into g
select new {Percentage = g.Key, Frequency = g.Count() };
// left join betweem the labels and the results with frequencies
var distributionWithLabels =
from l in rangesWithLabels
join r in resultsDistribution on l.Range equals r.Percentage
into rd
from r in rd.DefaultIfEmpty()
select new TestDistribution{
Label = l.Label,
Frequency = r != null ? r.Frequency : 0
};
distribution = distributionWithLabels.ToList();
Another solution if the ranges and labels can be created in another way
var ranges = Enumerable.Range(0, 10)
.Select(c=> new {
Min = c * 10,
Max = (c +1 )* 10,
Label = (c * 10) + "%'s"})
.Union(new[] { new {
Min = 100,
Max = Int32.MaxValue,
Label = ">110% "
}});
var resultsDistribution = from c in Results
group c by ranges.FirstOrDefault(r=> r.Min <= c.Percentage && c.Percentage < r.Max).Min
into g
select new {Percentage = g.Key, Frequency = g.Count() };
var distributionWithLabels =
from l in ranges
join r in resultsDistribution on l.Min equals r.Percentage
into rd
from r in rd.DefaultIfEmpty()
select new TestDistribution{
Label = l.Label,
Frequency = r != null ? r.Frequency : 0
};
This works
public IEnumerable<TestDistribution> GetDistribution()
{
var range = 12;
return Enumerable.Range(0, range).Select(
n => new TestDistribution
{
Label = string.Format("{1}{0}%'s", n*10, n==range-1 ? ">" : ""),
Frequency =
Results.Count(
d =>
d.Percentage >= n*10
&& d.Percentage < ((n == range - 1) ? decimal.MaxValue : (n+1)*10))
});
}

C# EF Linq bitwise question

Ok for example, I am using bitwise like such: Monday = 1, Tuesday = 2, Wednesday = 4, Thursday = 8 etc...
I am using an Entity Framework class of Business.
I am using a class and passing in a value like 7 (Monday, Tuesday, Wednesday).
I want to return records that match any of those days
public List<Business> GetBusinesses(long daysOfWeek)
{
using (var c = Context())
{
return c.Businesses.Where(???).ToList();
}
}
Any help would be appreciated. Thanks!
EDIT
Ok, so I am attempting the following:
var b = new List<Business>();
var b1 = new Business(){DaysOfWeek = 3};
b.Add(b1);
var b2 = new Business() { DaysOfWeek = 2 };
b.Add(b2);
var decomposedList = new[]{1};
var l = b.Where(o => decomposedList.Any(day => day == o.DaysOfWeek)).ToList();
But l returns 0 results assuming in the decomposedList(1) I am looking for monday.
I created b1 to contain Monday and Tuesday.
Use the bitwise and operator & to combine your desired flags with the actual flags in the database and then check for a non-zero result.
var b1 = new { DaysOfWeek = 3 };
var b2 = new { DaysOfWeek = 2 };
var b = new[] { b1, b2 };
var filter = 1;
var l = b.Where(o => (filter & o.DaysOfWeek) != 0);
foreach (var x in l)
{
Console.WriteLine(x);
}
If you have an array of filter values simply combined then with an OR | first:
var filterArray = new []{1, 4};
var filter = filterArray.Aggregate((x, y) => x | y);
You must decompose the long value(bitflagged enum will be better) to it's parts then pass it to Where
return c.Businesses.Where(o=> DecomposeDays(dayParam).Any(day => day==o)).ToList();
EDIT:
decompose method:
private static IEnumerable<byte> DecomposeDays(byte dayParam)
{
var daysOfWeek = new List<byte> { 1, 2, 4, 6, 8 ,16};
return daysOfWeek.Where(o => (o & dayParam) == dayParam);
}

Resources