while True:
if row_num == 3: break
rows = board[row_num]
string_of_row_checker = ""
col_num = 0
for cell in rows:
checker = {
"column checker": (list_of_column_checker[col_num] == cell and row_num == 2),
"row checker": (string_of_row_checker == cell and col_num == 2),
"left to right diagonal check": (diagonal_1 == cell and row_num == 2 and col_num == 2),
"right to left diagonal check": (diagonal_2 == cell and row_num == 2 and col_num == 0),
"row number": (row_num),
"column number": (col_num)
}
print(checker["row checker"])
#does print true when col num is and string_of_row_checker
match checker:
case {"row checker": True}:
#this case is never true even when it prints true above
return cell
case {"row number": 0}:
print("pass")
list_of_column_checker[col_num] = cell
if col_num == 0: diagonal_1 = cell
case {"row number": 1}:
if list_of_column_checker[col_num] != cell:
list_of_column_checker[col_num] = ""
if col_num == 1:
if diagonal_1 != cell: diagonal_1 = ""
if diagonal_2 != cell: diagonal_2 = ""
case {"row number": 2}:
if col_num == 0: diagonal_2 = cell
I'm trying to use the new match case statement for checking the winner in a tictactoe game
However, the case statement included above never runs even when "print(checker["row checker"])" prints true
How can I fix this
Thanks in Advance!
I have tried using the bool() function and converting the value stored in checker["row checker"] to string but that didn't work as well
All the other case statements work as they are supposed too except for the first one
I want to sort Letters first and followed by numbers like below:
[Austria , France , Germany , 101110 , 124563]
This is what i have tried:
obj.sort((a,b) => a.text > b.text ? 1 : -1)
But it is sorting numbers first and then letters.
Any help?
If you are looking for a solution like you want to sort string at the first portion of array and numbers at the last of array, just make sure that you are following this procedure.
Never compare a string with a number.
If both a and b are string or both are numbers just compare both with a > b and return 1 or -1 depending on the result of comparision.
The last else block corresponds to the comparision between a number and a string. The return value determines the location of the numbers in the array. If either one of them is not a number, return -1 or 1 depending on the requirement. Since you want the numbers at the end of your array, return -1 if the value is not a number. If you send 1 instead, the numbers will take the first posion in he output array.
const data = [101110 , 124563 , 'France' , 'Austria', 'Germany'];
const output = data.sort((a,b) => {
if (
(isNaN(a) && isNaN(b)) || (!isNaN(a) && !isNaN(b))
) {
// Both are strings
// OR
// Both are numbers
return a > b ? 1 : -1;
}
else {
// One of them is a number
return isNaN(a) ? -1 : 1;
}
});
console.log(output);
Much Simplified version
const data = [101110, 'France', 'Austria', 124563, 'Germany'];
const checkOfSamePattern = (a, b) => (isNaN(a) && isNaN(b)) || (!isNaN(a) && !isNaN(b));
const output = data.sort((a, b) => checkOfSamePattern(a, b) ? a > b ? 1 : -1 : isNaN(a) ? -1 : 1);
console.log(output);
public pageReference getData(){
date sd = date.parse(sDate).toStartOfWeek();
date ed = date.parse(eDate);
hList = new list<Hours__c>([SELECT Name,Employee__r.Id,Date_worked__c,Employee__r.Offshore__c,Employee__r.Name,Employee__r.Department__c,Employee_Name__c,hours__c,Minutes__c,Hours_Decimal__c FROM Hours__c WHERE Type_of_Hours__c = 'Work' AND Date_worked__c >= :sd AND Date_worked__c <= :ed AND Employee__r.Department__c IN :selected ORDER BY Employee__r.Department__c,Date_worked__c,Employee__r.Name LIMIT 3000]);
wrap = new list<wrapper>();
string tempDepartment;
string tempEmpName;
date tempWorkDate;
date tempStart = sd;
decimal offshore = 0;
decimal onshore = 0;
decimal totalOffshore = 0;
decimal totalOnShore = 0;
decimal dateCount = 0;
decimal aggreagteTotal = 0;
integer hitLimit = 3000;
integer rpwp = 0;
integer compareRPWP = 0;
boolean fired = false;
boolean dateChanged = false;
boolean dateCountis13 = false;
map<date,integer> mapDateCount = new map<date,integer>();
if(hList.size() > 0 && hList.size() < hitLimit){
noResults = false;
for(Hours__c h: hList){
//initiate a new wrapper for each department as a primary header
if( (h.Employee__r.Department__c != null && ((tempDepartment != null && tempDepartment != h.Employee__r.Department__c) || ( tempDepartment == null)) ) ){
fired = true;
tempDepartment = h.Employee__r.Department__c;
wrap.add(new wrapper(null,null,null,null,null,tempDepartment,'','','',null,'',''));
}else{
fired = false;
}
if(fired || dateCountis13){
dateCountis13 = false;
dateCount = 0;
totalOffshore = 0;
totalOnShore = 0;
}
//initiate a new wrapper for each new week period, total employee hours for each week period for each department
if(fired || (h.Date_worked__c != null && ((tempWorkDate != null && tempWorkDate != h.Date_worked__c) || ( tempWorkDate == null)) ) || (h.Employee__r.Department__c != null && ((tempDepartment != null && tempDepartment != h.Employee__r.Department__c) || ( tempDepartment == null)) ) ){
dateChanged = true;
tempWorkDate = h.Date_worked__c;
offshore = 0;
onshore = 0;
rpwp = 0;
dateCount++;
for(Hours__c h1: hList){
//get count of each record that is inside a given week period
if(h1.Date_worked__c == tempWorkDate && h1.Employee__r.Department__c == tempDepartment){
rpwp++;
}
//logic for totaling offshore hours by date and department
if(h1.Date_worked__c == tempWorkDate && h1.Employee__r.Offshore__c && h1.Employee__r.Department__c == tempDepartment){
offshore += h1.Hours_Decimal__c != null ? h1.Hours_Decimal__c : 0;
}
//logic for totaling onshore hours by date and department
if(h1.Date_worked__c == tempWorkDate && h1.Employee__r.Offshore__c == false && h1.Employee__r.Department__c == tempDepartment){
onshore += h1.Hours_Decimal__c != null ? h1.Hours_Decimal__c : 0;
}
}
system.debug('rpwp: ' + rpwp);
wrap.add(new wrapper(h.Date_worked__c.toStartOfWeek(),h.Date_worked__c,onshore,offshore,null,'total','','','',null,'',''));
totalOffshore += offshore;
totalOnShore += onshore;
system.debug('OffShore: ' + offshore + ' ' + tempWorkDate.format());
system.debug('OnShore: ' + onshore + ' ' + tempWorkDate.format());
}else{
dateChanged = false;
}
//initiate a new wrapper for each record and give the ability to open and collapse all records under a given total week period
// functionality now does an aggregate total of employee's that have multiple hour entrees for each week period.
if( dateChanged || (h.Employee__r.Name != null && ((tempEmpName != null && tempEmpName != h.Employee__r.Name) || ( tempEmpName == null)) ) ){
tempEmpName = h.Employee__r.Name;
aggreagteTotal = 0;
for(Hours__c h2: hList){
if(tempEmpName == h2.Employee__r.Name && tempWorkDate == h2.Date_worked__c){
aggreagteTotal += h2.Hours_Decimal__c;
}
}
if(h.Employee__r.Offshore__c){
wrap.add(new wrapper(h.Date_worked__c.toStartOfWeek(),h.Date_worked__c,0,h.hours__c,aggreagteTotal,'collapse',h.Employee__r.Name,h.Employee__r.Id,h.Name,h.Employee__r.Offshore__c,h.Employee__r.Department__c,h.Date_worked__c.format() ));
}else{
wrap.add(new wrapper(h.Date_worked__c.toStartOfWeek(),h.Date_worked__c,h.hours__c,0,aggreagteTotal,'collapse',h.Employee__r.Name,h.Employee__r.Id,h.Name,h.Employee__r.Offshore__c,h.Employee__r.Department__c,h.Date_worked__c.format() ));
}
}
//compare record count on the outside loop to record count on the inner loop and set compare iterator to 0 if the date changes
if(dateChanged){
compareRPWP = 0;
}
compareRPWP++;
system.debug('compareRPWP: ' + compareRPWP );
//if on the 13th week -- initiate a new wrapper and get quarterly totals -- compare record count so that inner loop matches outer loop
if( (dateCount == 13 && compareRPWP == rpwp) ){
system.debug('totalOffshore: ' + totalOffshore );
system.debug('totalOnShore: ' + totalOnShore );
dateCountis13 = true;
wrap.add(new wrapper(null,null,totalOnShore,totalOffshore,null,'quarter',tempDepartment,'','',null,'',''));
}
}
}else if(hList.size() <= 0){
noResults = true;
bannerMessage = 'Oops! No record found.';
}else if(hList.size() >= hitLimit){
noResults = true;
bannerMessage = 'Oops! 3000 Record limit hit.';
}else{
noResults = true;
bannerMessage = 'Oops! 50000 record limit hit.';
}
system.debug('wrap: ' + wrap);
system.debug('hList: ' + hList);
return null;
}
Here is my working method inside my class. Essentially I am doing a query on a custom object Hours__c. What I want to do is have the output on the table be first nested by each employee department. Then Nested by each work period, with all hours worked by that department for each work period. I essentially achieved this from the start by ordering the query that way. Now it was about how to make the logic grab it at the time I needed. But it also needed to aggregate the total hours if employees had more than one hour record per week period. I also finally after every 13 weeks output a total of hours for that quarter.
Here is a screencast of my custom report if you are interested in seeing what I achieved on the front-end but what I want to achieve on the backend and in a more effecient manner. The problem with my logic, is while it works, if there is a lot of data being queried. This logic is pretty slow. Because first it loops the query results once. but inside the first for loop I loop the same query 2 different times... ::slaps head:: ...I know it is terrible.
So I want to see maybe if there is a more effecient way of achieving this with potentially a map or another wrapper class? Because it is all in one wrapper class too. I use front end logic to sort out the table based on results in the wrapper class. I just hide and show rows. Like i said, it works just not as effecient as I would like my logic to run.
here is the screen cast:
https://screencast-o-matic.com/watch/cF60cwYAiB
My first idea is about preparing new Map<Date, List<Hour__c>> dateWorkedToHoursMap map in controller just after your SOQL. With this map you will be able to get quickly Hour__c records by specic Date (Date_worked__c) and go only through them in your sub for loops.
public class Controller {
// (...)
Map<Date, List<Hour__c>> dateWorkedToHoursMap; // New map in controller
// (...)
// Updated "getData()" method which will prepare "dateWorkedToHoursMap"
// map just after SOQL and then will use in both sub for loops
public pageReference getData() {
// (...)
// Your SOQL for Hour__c records
// dateWorkedToHoursMap = prepareDateWorkedToHoursMap(hList);
// (...)
// main for through hList
// (...)
// 1 sub for through list returned by: dateWorkedToHoursMap.get(h.Date_worked__c)
// (...)
// 2 sub for through list returned by: dateWorkedToHoursMap.get(h.Date_worked__c)
// (...)
// end of main for
}
// New "prepareDateWorkedToHoursMap()" method used to prepare "dateWorkedToHoursMap" map
private void prepareDateWorkedToHoursMap(List<Hour__c> hours) {
dateWorkedToHoursMap = new Map<Date, List<Hour__c>>();
for(Hour__c hour : hours) {
if(hour.Date_worked__c != null) {
if(!dateWorkedToHoursMap.containsKey(hour.Date_worked__c)) {
dateWorkedToHoursMap.put(hour.Date_worked__c, new List<Hour__c>());
}
dateWorkedToHoursMap.get(hour.Date_worked__c).add(h);
}
}
}
}
If you think that it will be better then you can try to prepare map like:
Map<String, Map<Date, List<Hour__c>>> departmentToDateWorkedToHoursMap
And then you should be able to quickly get the list of Hour__c records based on DEPARTMENT and DATE WORKED.
I have a small perl script that needs to evaluate the equality of two parameters and a small return from the database.
my ($firstId, $secondId, $firstReturnedId, $secondReturnedId, $picCount);
my $pics = $dbh->prepare(qq[select id from pictures limit 10]);
$firstId = q->param('firstId');
$secondId = q->param('secondId');
$pics->execute or die;
my $picids = $pics->fetchall_arrayref;
$picCount = scalar(#{$picids});
$firstReturnedId = $picCount > 0 ? shift(#{$picids}) : 0;
$secondReturnedId = $picCount > 1 ? pop(#{$picids}) : $firstReturnedId;
Here, a quick look at my debugger shows that $picCount = 1 and $firstReturnedId = 9020 and $secondReturnedId = 9020. However, they are both denoted as
ARRAY(0x9e79184)
0 9020
in the debugger so when I perform the final check
my $result = (($firstId == $firstReturnedId) && ($secondId == $secondReturnedId)) ? 1 : 0;
I get $result = 0, which is not what I want.
What am I doing wrong?
DBI::fetchall_arrayref returns a reference to a list of "row results". But since there could be more than one value in a row result (e.g., your query could have been select id,other_field from pictures), each row result is also a reference to a list. This means you have one more dereferencing to do in order to get the result you want. Try:
$picCount = scalar(#{$picids});
if ($picCount > 0) {
my $result = shift #{$picids};
$firstReturnedId = $result->[0];
} else {
$firstReturnedId = 0;
}
if ($picCount > 1) {
my $result = pop #{$picids};
$secondReturnedId = $result->[0];
} else {
$secondReturnedId = $firstReturnedId;
}
or if you still want to use a concise style:
$firstReturnedId = $picCount > 0 ? shift(#{$picids})->[0] : 0;
$secondReturnedId = $picCount > 1 ? pop(#{$picids})->[0] : $firstReturnedId;
I have a winforms screen and the below code in Form_Activated event.
if (genlGrid1.Rows.Count > 0 && genlGrid1.Rows.Count <= genlGridIndex + 1 && (genlGridIndex >= 0 && genlGridIndex < 2))
{
//Looks like below line is where exception occurs
genlGrid1.Rows[genlGridIndex].Cells[0].Selected = true;
}
I'm very sure that the grid has 2 rows and 9 cells when this line of code is executed.
When this line of code is executed, I get the error below. No idea what is going on with the grid. What is SetCurrentCellAddressCore and why are we getting this exception??
Can some one please help?
System.ArgumentOutOfRangeException:
Specified argument was out of the
range of valid values. Parameter name:
columnIndex at
System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32
columnIndex, Int32 rowIndex, Boolean
setAnchorCellAddress, Boolean
validateCurrentCell, Boolean
throughMouseClick) at
System.Windows.Forms.DataGridView.SetSelectedCellCoreInternal(Int32
columnIndex, Int32 rowIndex, Boolean
selected) at
System.Windows.Forms.DataGridViewCell.set_Selected(Boolean
value)
Should it be
if (genlGrid1.Rows.Count > 0 &&
genlGrid1.Rows.Count >= genlGridIndex + 1 &&
(genlGridIndex >= 0 && genlGridIndex < 2))
{
genlGrid1.Rows[genlGridIndex].Cells[0].Selected = true;
}
?
Note genlGrid1.Rows.Count >= genlGridIndex + 1