Split String not Meeting Conditions [Swift] - if-statement

I'm trying to take a series of numbers from a text file and based on the numbers define a new variable. When I print SettingsArray i get the following:
[10, 25, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0.02, 0.002, 0, 1, 0, 0, 0, 1, 1, 1, 25, 500, 0, 1, 250, 250, 250, 500, 500, 500, 10, 10, 10, 200, 200, 200]
Which are the numbers I'm looking for, but for some reason, when checking the If statements, all of the SettingsArray elements look something like this:
[2] String class name = _TtC10Foundation15_NSOpaqueString
Which obviously don't meet any of the conditions, why are the array elements garbled when checking the If statements?
let file = String(contentsOfFile: "/Users/UserGoesHere/Documents/Settings.txt", encoding: NSUTF8StringEncoding, error: nil)!
var SettingsArray = split(file) {$0 == ","}
var StepPortInvert = ""
if SettingsArray[2] == "1" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
var StepPortInvert = "00000000"
}
if SettingsArray[2] == "0" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
var StepPortInvert = "00000001"
}
if SettingsArray[2] == "1" && SettingsArray[3] == "0" && SettingsArray[4] == "1" {
var StepPortInvert = "00000010"
}

You just have to declare StepPortInvert once. BTW 2,3,4 = "1, 0, 0"
let file = String(contentsOfFile: "/Users/UserGoesHere/Documents/Settings.txt", encoding: NSUTF8StringEncoding, error: nil)!
var SettingsArray = split(file) {$0 == ","}
var StepPortInvert = ""
if SettingsArray[2] == "1" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
StepPortInvert = "00000000"
}
if SettingsArray[2] == "0" && SettingsArray[3] == "1" && SettingsArray[4] == "1" {
StepPortInvert = "00000001"
}
if SettingsArray[2] == "1" && SettingsArray[3] == "0" && SettingsArray[4] == "1" {
StepPortInvert = "00000010"
}

Related

Else If - Google Script

this script was working before on my Google Sheet, then all of a sudden, it stopped working. I notice that the first argument (if) is working, but all the codes after that is not working. Any ideas?
function onEdit(event) {
// assumes source data in sheet named Sheet1
// target sheet of move to named Sheet2
// test column with yes is col 7 or G
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Under Contract Response Form" && r.getColumn() == 7 && (r.getValue() == "Closed" || r.getValue() == "Expired" || r.getValue() == "Cancelled")) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Closed/Expired & Cancelled");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row);
} else if(s.getName() == "Listing Response Form" && r.getColumn() == 7 && r.getValue() == "Pending" ) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Under Contract Response Form");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row);
} else if(s.getName() == "Listing Response Form" && r.getColumn() == 7 && r.getValue() == "Cancelled" ) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Closed/Expired & Cancelled");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row);
} else if(s.getName() == "Under Contract Response Form" && r.getColumn() == 7 && (r.getValue() == "Pre-list" || r.getValue() == "Withheld" || r.getValue() == "Coming Soon" || r.getValue() == "Active")) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Listing Response Form");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row);
} else if(s.getName() == "Closed/Expired & Cancelled" && r.getColumn() == 7 && r.getValue() == "Pending" ) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Under Contract Response Form");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row);
}
}
The script looks fine, so chances are that one or more sheets has been renamed, or columns inserted or deleted. Check the sheet names and look for things like leading or trailing spaces. Also ensure that the values that should trigger row move appear in column G.
You can make row moves more robust by using column names instead of column numbers. See the moveRowsFromSpreadsheetToSpreadsheet_ script.

If statement Debug

how I'm going to fix the if-statement on line 31. It just don't want to work. I'm using a source code that enables me to easy create 3D objects. Here's the code:
<script src="http://koda.nu/simple3d.js">
ambientLight("white");
var debug = false;
var player = {x: 0, y: 0, z: 500, item: box(0, 0, 500, 200, 200, 1, "red"), speed: 7};
var ball = {x: 0, y: 0, z: -200, item: sphere(0, 0, -200, 100, "yellow"), xSpeed: 0, ySpeed: 0, zSpeed: -15};
var enemy = {x: 0, y: 0, z: -700, item: box(0, 0, -700, 200, 200, 1, "red"), speed: 7};
box(450, 0, -100, 1, 875, 1200, "blue");
box(-450, 0, -100, 1, 875, 1200, "blue");
box(0, 450, -100, 875, 1, 1200, "blue");
box(0, -450, -100, 875, 1, 1200, "blue");
function update() {
if (keyboard.w && player.y < 350) {
player.item.translateY(player.speed);
player.y += player.speed;
}
if (keyboard.s && player.y > -350) {
player.item.translateY(-player.speed);
player.y -= player.speed;
}
if (keyboard.d && player.x < 350) {
player.item.translateX(player.speed);
player.x += player.speed;
}
if (keyboard.a && player.x > -350) {
player.item.translateX(-player.speed);
player.x -= player.speed;
}
/*(This line just don't want to work ---->)*/ if (ball.z == enemy.z) { ball.zSpeed *= -1; }
if (keyboard.shift && keyboard.y) {
debug = true;
}
ball.x += ball.xSpeed;
ball.item.translateX(ball.xSpeed);
ball.y += ball.ySpeed;
ball.item.translateY(ball.ySpeed);
ball.z += ball.zSpeed;
ball.item.translateZ(ball.zSpeed);
while (debug) {
if (keyboard.up) {
moveCameraZ(-player.speed);
}
if (keyboard.down) {
moveCameraZ(player.speed);
}
if (keyboard.left) {
moveCameraX(-player.speed);
}
if (keyboard.right) {
moveCameraX(player.speed);
}
if (keyboard.shift && keyboard.y) {
debug = True;
}
}
}
</script>
your code is very messy, i think it must be instead of { debug = True; } it must be { debug = False; }

Replace numbers to 3 decimal places using RegEx

I need to replace only numbers that are decimal to 3 places.
The following example is working fine.
Output look like this:
0.000
But i can type 0..
How can i do only one decimal point (.) 0.000
Here is my directive:
app.directive('allowDecimalNumbers', function () {
return {
restrict: 'A',
link: function (scope, elm, attrs, ctrl) {
elm.on('keypress', function (event) {
var $input = $(this);
var value = $input.val();
value = value.replace(/[^0-9\.]/g, '')
if(value == "" && event.which == 46) {
return false;
}
var findsDot = new RegExp(/\./g)
var containsDot = value.match(findsDot)
if (containsDot != null && ([46, 110, 190].indexOf(event.which) > -1)) {
event.preventDefault();
return false;
}
var arrValue = value.split('.');
if (value.split('.').length == 2) {
if(value.split('.')[1].length > 2) {
event.preventDefault();
return false;
}
}
$input.val(value);
if (event.which == 64 || event.which == 16) {
// numbers
return false;
}
else if (event.which >= 48 && event.which <= 57 || event.which == 46) {
// numbers
return true;
}
else {
event.preventDefault();
return false;
}
});
}
}});
Here is my html:
<input type="number" allow-decimal-numbers ng-model="length1" >
Note that validation can cause problem depending on locale, in french decimal separator is , and input type="number" prevents . to be typed.
could make it working removing code, note that . doesn't need to be escaped when it is in a character set ( between [ and ])
var app = angular.module('app', []);
app.directive('allowDecimalNumbers', function () {
return {
restrict: 'A',
link: function (scope, elm, attrs, ctrl) {
elm.on('keydown', function (event) {
var $input = $(this);
var value = $input.val();
if ([8, 13, 16, 27, 37, 38, 39, 40, 46].indexOf(event.which) > -1) {
// backspace, enter, shift, escape, arrows, delete
return true;
} else if ( (event.which >= 48 && event.which <= 57 ||
event.which >= 96 && event.which <= 105) && !value.match(/[.,]\d{3}/)) {
// numbers
return true;
} else if ([46, 110, 190, 188].indexOf(event.which) > -1 && !value.match(/[.,]/)) {
// dot and numpad dot
return true;
} else {
event.preventDefault();
return false;
}
});
}
}
});
Note that a digit can't be inserted after there's a decimal separator followed by three digit but we can't check where the digit is inserted even if it's in front.

How to make a game over sequence for tic tac toe when there are no winners?

I would like to display a label that says game over when there are no winners. I just thought to make an else statement but once there is one click game over would be display since the elif statements are false. Any ideas.
from tkinter import*
#Window stuff
window = Tk()
window.title("Tic Tac Toe")
window.configure(background = "black")
window.geometry("400x400")
#Variables
global clickable
playerXturn = True
ticker = 0
#Display X or O
def buttonClicked(c) :
global playerXturn
if playerXturn == True :
buttonList[c]["image"] = picX
buttonList[c]["state"] = DISABLED
playerXturn = False
labelTurn ["text"] = "O's turn"
elif clickable[c] == "" :
buttonList[c]["image"] = picO
buttonList[c]["state"] = DISABLED
playerXturn = True
labelTurn ["text"] = "X's turn"
#Info for user
global ticker
ticker += 1
if ticker == 500 :
labelInfo["text"] = "Timed Out"
labelInfo["bg"] = "red"
#Three in a row
elif (button1["image"] == str(picX) and button2["image"] == str(picX) and button3["image"] == str(picX) or
button4["image"] == str(picX) and button5["image"] == str(picX) and button6["image"] == str(picX) or
button7["image"] == str(picX) and button8["image"] == str(picX) and button9["image"] == str(picX) or
button1["image"] == str(picX) and button4["image"] == str(picX) and button7["image"] == str(picX) or
button2["image"] == str(picX) and button5["image"] == str(picX) and button8["image"] == str(picX) or
button3["image"] == str(picX) and button6["image"] == str(picX) and button9["image"] == str(picX) or
button1["image"] == str(picX) and button5["image"] == str(picX) and button9["image"] == str(picX) or
button3["image"] == str(picX) and button5["image"] == str(picX) and button7["image"] == str(picX)) :
#print ("X")
labelInfo["text"] = "X Wins"
labelInfo["bg"] = "red"
button1["state"] = DISABLED
button2["state"] = DISABLED
button3["state"] = DISABLED
button4["state"] = DISABLED
button5["state"] = DISABLED
button6["state"] = DISABLED
button7["state"] = DISABLED
button8["state"] = DISABLED
button9["state"] = DISABLED
labelTurn ["bg"] = "black"
elif (button1["image"] == str(picO) and button2["image"] == str(picO) and button3["image"] == str(picO) or
button4["image"] == str(picO) and button5["image"] == str(picO) and button6["image"] == str(picO) or
button7["image"] == str(picO) and button8["image"] == str(picO) and button9["image"] == str(picO) or
button1["image"] == str(picO) and button4["image"] == str(picO) and button7["image"] == str(picO) or
button2["image"] == str(picO) and button5["image"] == str(picO) and button8["image"] == str(picO) or
button3["image"] == str(picO) and button6["image"] == str(picO) and button9["image"] == str(picO) or
button1["image"] == str(picO) and button5["image"] == str(picO) and button9["image"] == str(picO) or
button3["image"] == str(picO) and button5["image"] == str(picO) and button7["image"] == str(picO)) :
#print("O")
labelInfo["text"] = "O Wins"
labelInfo["bg"] = "red"
button1["state"] = DISABLED
button2["state"] = DISABLED
button3["state"] = DISABLED
button4["state"] = DISABLED
button5["state"] = DISABLED
button6["state"] = DISABLED
button7["state"] = DISABLED
button8["state"] = DISABLED
button9["state"] = DISABLED
labelTurn ["bg"] = "black"
#Images
picX = PhotoImage (file = "x.gif")
picO = PhotoImage (file = "o.gif")
picBlank = PhotoImage (file = "sw.gif")
#Buttons
button1 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(0))
button1.grid (row = 0, column = 0)
#button1["state"] = DISABLED
button2 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(1))
button2.grid (row = 0, column = 1)
#button2["state"] = DISABLED
button3 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(2))
button3.grid (row = 0, column = 2)
#button3["state"] = DISABLED
button4 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(3))
button4.grid (row = 1, column = 0)
#button4["state"] = DISABLED
button5 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(4))
button5.grid (row = 1, column = 1)
#button5["state"] = DISABLED
button6 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(5))
button6.grid (row= 1, column = 2)
#button6["state"] = DISABLED
button7 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(6))
button7.grid (row = 2, column = 0)
#button7["state"] = DISABLED
button8 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(7))
button8.grid (row = 2, column = 1)
#button8["state"] = DISABLED
button9 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(8))
button9.grid (row = 2, column = 2)
#button9["state"] = DISABLED
#Lists
buttonList = [button1, button2, button3, button4, button5, button6, button7, button8, button9]
clickable = ["", "", "", "", "", "", "", "", ""]
#Reset
def processReset():
button1["state"] = NORMAL
button2["state"] = NORMAL
button3["state"] = NORMAL
button4["state"] = NORMAL
button5["state"] = NORMAL
button6["state"] = NORMAL
button7["state"] = NORMAL
button8["state"] = NORMAL
button9["state"] = NORMAL
button1["image"] = picBlank
button2["image"] = picBlank
button3["image"] = picBlank
button4["image"] = picBlank
button5["image"] = picBlank
button6["image"] = picBlank
button7["image"] = picBlank
button8["image"] = picBlank
button9["image"] = picBlank
labelTurn["bg"] = "white"
labelInfo["text"] = "Continue Playing"
labelInfo["bg"] = "white"
#Extra labels and buttons
labelMenu = Label (window, text = "Menu and Info", height = 2, width = 24, bg = "yellow")
labelMenu.grid (row = 4, column = 4)
labelTurn = Label (window, text = "X goes first", height = 2, width = 10)
labelTurn.grid (row = 6, column = 4)
labelInfo = Label (window, text = "Continue Playing", height = 2, width = 14)
labelInfo.grid (row = 5, column = 4)
buttonReset = Button(window, text = "Reset/New Game", bg = "Orange", command = processReset)
buttonReset.grid (row = 7, column = 4)
labelNote = Label (window, text = "Note: Losing player goes first next game", bg = "Pink")
labelNote.grid (row = 8, column = 4)
window.mainloop()
You want an elif statement at the end of your else-if chain that checks whether there's any empty spaces left. If there's no spaces left, and a winner wasn't declared by one of the statements before this one, it's necessarily a tie.
Something like:
elif (check that no empty board spaces remain) :
#print("-")
labelInfo["text"] = "It's a tie!"

Drill down in google charts

I am trying to implement a drill in or drill down in a pie chart. I actually have a working drill down pie chart, however, when I changed the values of the collection, it did not work. I am wondering what went wrong as I completely followed the working code and just replaced its values. The chart simply does not show up and has an error: Uncaught Error: Unknown header type: 17format+en,default+en,ui+en,controls+en,corechart+en.I.js:191. I am not sure though whether this error is related to the problem.
Javascript:
google.load('visualization', '1', {packages: ['corechart', 'controls']});
google.setOnLoadCallback(drawChart1);
var index = 0;
function drawChart1() {
<%
int aku = 0, cdu = 0, ls = 0, ptr = 0, rad = 0, oper = 0;
int aku1 = 0, aku2 = 0, aku3 = 0, aku4 = 0, aku5 = 0;
int cdu1 = 0, cdu2 = 0, cdu3 = 0, cdu4 = 0, cdu5 = 0, cdu6 = 0;
int ls1 = 0, ls2 = 0, ls3 = 0, ls4 = 0, ls5 = 0, ls6 = 0, ls7 = 0, ls8 = 0, ls9 = 0, ls10 = 0;
int ptr1 = 0, ptr2 = 0, ptr3 = 0, ptr4 = 0;
int rad1 = 0, rad2 = 0, rad3 = 0;
int oper1 = 0;
%> //Dummy values
//Main
var main = [
['Artificial Kidney Unit', <%=aku%>],
['Cardiac Diagnostic Unit', <%=cdu%>],
['Laboratory Services', <%=ls%>],
['Physical Therapy and Rehabilitation', <%=ptr%>],
['Radiology', <%=rad%>],
['Operations', <%=oper%>]
];
//Aku
var akuu = [
['Hemodialysis', <%=aku1%>],
['Peritoneal Dialysis', <%=aku2%>],
['Continuous Renal Replacement Therapy', <%=aku3%>],
['Sustained Low Efficient Dialysis', <%=aku4%>],
['Private Dialysis Suite', <%=aku5%>]
];
//Cdu
var cduu = [
['Electrocardiography', <%=cdu1%>],
['Ambulatory Electrocardiography', <%=cdu2%>],
['Exercise Stress Test', <%=cdu3%>],
['2D Echo', <%=cdu4%>],
['Lower Extremity Arterial & Venous Color Duplex Scan', <%=cdu5%>],
['Carotid Artery Duplex Scan', <%=cdu6%>]
];
//Ls
var lss = [
['Hematology', <%=ls1%>],
['Blood Chemistry', <%=ls2%>],
['Immunology and Serology', <%=ls3%>],
['Clinical Microscopy', <%=ls4%>],
['Microbiology', <%=ls5%>],
['Blood Bank and Transfusion Services', <%=ls6%>],
['Drug Testing', <%=ls7%>],
['Parasitology', <%=ls8%>],
['Surgical Pathology', <%=ls9%>],
['Cytopathology', <%=ls10%>]
];
//Ptr
var ptrr = [
['Physical Therapy', <%=ptr1%>],
['Occupational Therapy', <%=ptr2%>],
['Ultrasound Diagnostic Therapy', <%=ptr3%>],
['Orthotics and Prosthetic Evaluation', <%=ptr4%>]
];
//rad
var radd = [
['X-ray', <%=rad1%>],
['Ultrasound', <%=rad2%>],
['CT Scan', <%=rad3%>]
];
//oper
var operr = [
['Surgery', <%=oper1%>]
];
var collection = [];
collection[0] = google.visualization.arrayToDataTable(main);
collection[1] = google.visualization.arrayToDataTable(akuu);
collection[2] = google.visualization.arrayToDataTable(cduu);
collection[3] = google.visualization.arrayToDataTable(lss);
collection[4] = google.visualization.arrayToDataTable(ptrr);
collection[5] = google.visualization.arrayToDataTable(radd);
collection[6] = google.visualization.arrayToDataTable(operr);
var options1 = {
title: 'Departments',
animation: {'duration': 500,
'easing': 'in'},
action: function() {
button.onclick = function() {
recreateDashboard(0);
};
}
};
var chart1 = new google.visualization.PieChart(document.getElementById('chart1'));
google.visualization.events.addListener(chart1, 'select', drillIn);
google.visualization.events.addListener(chart1, 'click', drillOut);
chart1.draw(collection[0], options1);
function drillIn() {
var sel = chart1.getSelection();
var row = chart1.getSelection()[0].row;
options1['title'] = collection[index].getValue(sel[0].row, 0);
if(index === 0) {
if(row === 0) {
index = 1;
}
if(row === 1) {
index = 2;
}
if(row === 2) {
index = 3;
}
if(row === 3) {
index = 4;
}
if(row === 4) {
index = 5;
}
if(row === 5) {
index = 6;
}
}
else if(index === 1 || index === 2 || index === 3 || index === 4 || index === 5 || index === 6) {
options1['title'] = '# of services rendered in <%=year%>';
index = 0;
}
chart1.draw(collection[index], options1);
}
function drillOut(e) {
if(e.targetID === "title") {
if(index !== 0)
index--;
else if(index === 4 || index === 6 || index === 8)
index -= 2;
chart1.draw(collection[index], options1);
}
}
Html:
<div id="chart1">
</div>
I have figured out the mistake. All of these need a title before inputting the values.
Revised code:
//Main
var main = [
['Department', 'Value'],
['Cardiac Diagnostic Unit', <%=cdu%>],
['Laboratory Services', <%=ls%>],
['Physical Therapy and Rehabilitation', <%=ptr%>],
['Radiology', <%=rad%>],
['Operations', <%=oper%>]
];
//Aku
var akuu = [
['Service', 'Value'],
['Hemodialysis', <%=aku1%>],
['Peritoneal Dialysis', <%=aku2%>],
['Continuous Renal Replacement Therapy', <%=aku3%>],
['Sustained Low Efficient Dialysis', <%=aku4%>],
['Private Dialysis Suite', <%=aku5%>]
];
//Cdu
var cduu = [
['Service', 'Value'],
['Electrocardiography', <%=cdu1%>],
['Ambulatory Electrocardiography', <%=cdu2%>],
['Exercise Stress Test', <%=cdu3%>],
['2D Echo', <%=cdu4%>],
['Lower Extremity Arterial & Venous Color Duplex Scan', <%=cdu5%>],
['Carotid Artery Duplex Scan', <%=cdu6%>]
];
//Ls
var lss = [
['Service', 'Value'],
['Hematology', <%=ls1%>],
['Blood Chemistry', <%=ls2%>],
['Immunology and Serology', <%=ls3%>],
['Clinical Microscopy', <%=ls4%>],
['Microbiology', <%=ls5%>],
['Blood Bank and Transfusion Services', <%=ls6%>],
['Drug Testing', <%=ls7%>],
['Parasitology', <%=ls8%>],
['Surgical Pathology', <%=ls9%>],
['Cytopathology', <%=ls10%>]
];
//Ptr
var ptrr = [
['Service', 'Value'],
['Physical Therapy', <%=ptr1%>],
['Occupational Therapy', <%=ptr2%>],
['Ultrasound Diagnostic Therapy', <%=ptr3%>],
['Orthotics and Prosthetic Evaluation', <%=ptr4%>]
];
//rad
var radd = [
['Service', 'Value'],
['X-ray', <%=rad1%>],
['Ultrasound', <%=rad2%>],
['CT Scan', <%=rad3%>]
];
//oper
var operr = [
['Service', 'Value'],
['Surgery', <%=oper1%>]
];